Share via


WriterLock Class

Helper class that makes it easier to ensure proper usage of a ReaderWriterLock for writers by providing support for IDisposable and the using keyword.

Namespace: Microsoft.Practices.CompositeUI
Assembly: Microsoft.Practices.CompositeUI (in microsoft.practices.compositeui.dll)

Syntax

'Declaration
Public Class WriterLock
    Inherits LockBase
'Usage
Dim instance As WriterLock
public class WriterLock : LockBase
public ref class WriterLock : public LockBase
public class WriterLock extends LockBase
public class WriterLock extends LockBase

Example

Common usage is as follows:

            ReaderWriterLock mylock = new ReaderWriterLock();
            
            // Inside some method
            using (new WriterLock(rwLock))
            {
                // Do your safe resource write accesses here.
            }
            

If a timeout is specified, the TimedOut property should be checked inside the using statement:

            ReaderWriterLock mylock = new ReaderWriterLock();
            
            // Inside some method
            using (WriterLock l = new WriterLock(rwLock, 100))
            {
                if (l.TimedOut == false)
                {
                    // Do your safe resource write accesses here.
                }
                else
                {
                    throw new InvalidOperationException("Could not lock the resource.");
                }
            }
            

Inheritance Hierarchy

System.Object
   Microsoft.Practices.CompositeUI.LockBase
    Microsoft.Practices.CompositeUI.WriterLock

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

See Also

Reference

WriterLock Members
Microsoft.Practices.CompositeUI Namespace