ReaderWriterLockSlim.EnterReadLock Method
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Tries to enter the lock in read mode.
public:
void EnterReadLock();
public void EnterReadLock ();
member this.EnterReadLock : unit -> unit
Public Sub EnterReadLock ()
The RecursionPolicy property is NoRecursion, and the current thread has attempted to acquire the read lock when it already holds the read lock.
-or-
The RecursionPolicy property is NoRecursion, and the current thread has attempted to 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 this exception.
The ReaderWriterLockSlim object has been disposed.
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.
private ReaderWriterLockSlim cacheLock = new ReaderWriterLockSlim();
private Dictionary<int, string> innerCache = new Dictionary<int, string>();
Private cacheLock As New ReaderWriterLockSlim()
Private innerCache As New Dictionary(Of Integer, String)
public string Read(int key)
{
cacheLock.EnterReadLock();
try
{
return innerCache[key];
}
finally
{
cacheLock.ExitReadLock();
}
}
Public Function Read(ByVal key As Integer) As String
cacheLock.EnterReadLock()
Try
Return innerCache(key)
Finally
cacheLock.ExitReadLock()
End Try
End Function
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.
Σημείωση
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.
Προϊόν | Εκδόσεις |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 |
.NET Framework | 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1 |
.NET Standard | 1.0, 1.1, 1.2, 1.3, 1.4, 1.6, 2.0, 2.1 |
UWP | 10.0 |
.NET σχόλια
.NET είναι ένα έργο ανοιχτού κώδικα. Επιλέξτε μια σύνδεση για να παρέχετε σχόλια: