CSharpTest.Net
TryRead Method
See Also  Example Send Feedback Download Help File
CSharpTest.Net.Library Assembly > CSharpTest.Net.Synchronization Namespace > ILockStrategy Interface : TryRead Method

timeout

Glossary Item Box

Returns true if the lock was successfully obtained within the timeout specified

Syntax

Visual Basic (Declaration) 
Function TryRead( _
   ByVal timeout As Integer _
) As Boolean
C# 
bool TryRead( 
   int timeout
)

Parameters

timeout

Example

Library/Library.Test/LockingTests/TestReservedWriteLocking.cs

C#Copy Code
using (ILockStrategy l = LockFactory.Create())
{
    Assert.AreEqual(0, l.WriteVersion);

    Assert.IsTrue(l.TryRead(0));
    l.ReleaseRead();
    Assert.AreEqual(0, l.WriteVersion);

    Assert.IsTrue(l.TryWrite(0));
    Assert.AreEqual(0, l.WriteVersion);
    l.ReleaseWrite();
    Assert.AreEqual(0, l.WriteVersion);

    using (l.Write())
    {
        Assert.AreEqual(0, l.WriteVersion);
        Assert.IsTrue(l.TryWrite(0));
        // Once a nested write lock is acquired the real lock is obtained.
        Assert.AreEqual(1, l.WriteVersion);
        l.ReleaseWrite();
        Assert.AreEqual(1, l.WriteVersion);
    }

    Assert.IsTrue(l.TryWrite(0));
    l.ReleaseWrite();
    Assert.AreEqual(1, l.WriteVersion);
}
VB.NETCopy Code
Using l As ILockStrategy = LockFactory.Create()
    Assert.AreEqual(0, l.WriteVersion)

    Assert.IsTrue(l.TryRead(0))
    l.ReleaseRead()
    Assert.AreEqual(0, l.WriteVersion)

    Assert.IsTrue(l.TryWrite(0))
    Assert.AreEqual(0, l.WriteVersion)
    l.ReleaseWrite()
    Assert.AreEqual(0, l.WriteVersion)

    Using l.Write()
        Assert.AreEqual(0, l.WriteVersion)
        Assert.IsTrue(l.TryWrite(0))
        ' Once a nested write lock is acquired the real lock is obtained.
        Assert.AreEqual(1, l.WriteVersion)
        l.ReleaseWrite()
        Assert.AreEqual(1, l.WriteVersion)
    End Using

    Assert.IsTrue(l.TryWrite(0))
    l.ReleaseWrite()
    Assert.AreEqual(1, l.WriteVersion)
End Using

Requirements

Target Platforms: Windows XP, Windows Server 2003, Windows Vista, Windows Server 2008, Windows 7

See Also

Generated with Document! X 2011 by Innovasys