Export (0) Print
Expand All

ReaderWriterLockSlim.EnterReadLock Method

Tries to enter the lock in read mode.

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

member EnterReadLock : unit -> unit

ExceptionCondition
LockRecursionException

The RecursionPolicy property is LockRecursionPolicy.NoRecursion and the current thread has already entered read mode.

-or-

The current thread may not acquire the read lock when it already holds the write lock.

-or-

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

ObjectDisposedException

The ReaderWriterLockSlim object has been disposed.

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

Multiple threads can enter read mode at the same time.

If one or more threads are waiting to enter write mode, a thread that calls the EnterReadLock method blocks until those threads 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 read mode can enter read mode recursively, even if other threads are waiting to enter write mode.

At most one thread can be in upgradeable mode while other threads are in read mode. If additional threads are waiting to enter upgradeable mode, and there are no threads waiting to enter write mode, threads that call the EnterReadLock method enter read mode immediately and do not block.

The following example shows how to use the EnterReadLock method to enter the lock in read mode. The method shown in the example retrieves the value associated with a key. If the key is not found, the exception thrown by the inner Dictionary(TKey, TValue) is allowed to terminate the method. A finally block is used to execute the ExitReadLock method, ensuring that the caller exits read mode.

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

No code example is currently available or this language may not be supported.

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8
Show:
© 2015 Microsoft