Export (0) Print
Expand All

ReaderWriterLockSlim.EnterWriteLock Method

Tries to enter the lock in write mode.

Namespace:  System.Threading
Assembly:  System.Core (in System.Core.dll)

public void EnterWriteLock()

ExceptionCondition
LockRecursionException

The RecursionPolicy property is LockRecursionPolicy.NoRecursion and the current thread has already entered the lock in any mode.

-or-

The current thread has entered read mode, so trying to enter the lock in write mode would create the possibility of a deadlock.

-or-

The recursion number would exceed the capacity of the counter. The limit is so large that applications should never encounter it.

This method blocks until the calling thread enters the lock, and therefore might never return. Use the TryEnterWriteLock method to block for a specified interval, and then return if the calling thread has not entered write mode during that interval.

If other threads have entered the lock in read mode, a thread that calls the EnterWriteLock method blocks until those threads have exited read mode. When there are threads waiting to enter write mode, additional threads that try to enter read mode or upgradeable mode block until all the threads waiting to enter write mode have either timed out or entered write mode and then exited from it.

NoteNote

If a lock allows recursion, a thread that has entered the lock in write mode can enter write mode recursively, even if other threads are waiting to enter write mode.

The following example shows how to use the EnterWriteLock method to enter the lock in write mode. The method shown in the example adds a new key/value pair to the synchronized cache. If the key is already in the cache, the exception thrown by the inner Dictionary<TKey, TValue> is allowed to terminate the method. A finally block is used to execute the ExitWriteLock method, ensuring that the caller exits write mode.

This code is part of a larger example provided for the ReaderWriterLockSlim class.

private ReaderWriterLockSlim cacheLock = new ReaderWriterLockSlim();
private Dictionary<int, string> innerCache = new Dictionary<int, string>();


...


public void Add(int key, string value)
{
    cacheLock.EnterWriteLock();
    try
    {
        innerCache.Add(key, value);
    }
    finally
    {
        cacheLock.ExitWriteLock();
    }
}

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft