ReaderWriterLockSlim.EnterReadLock 方法

定義

嘗試進入讀取模式的鎖定。Tries to enter the lock in read mode.

public:
 void EnterReadLock();
public void EnterReadLock ();
member this.EnterReadLock : unit -> unit
Public Sub EnterReadLock ()

例外狀況

RecursionPolicy 屬性是 NoRecursion,且目前的執行緒嘗試在已經保留讀取鎖定的情況下取得讀取鎖定。The RecursionPolicy property is NoRecursion, and the current thread has attempted to acquire the read lock when it already holds the read lock.

-或--or- RecursionPolicy 屬性是 NoRecursion,且目前的執行緒嘗試在已經保留寫入鎖定的情況下取得讀取鎖定。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.

ReaderWriterLockSlim 物件已經處置。The ReaderWriterLockSlim object has been disposed.

範例

下列範例顯示如何使用EnterReadLock方法, 以讀取模式進入鎖定。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. 如果找不到索引鍵, 則會允許內部Dictionary<TKey,TValue>擲回的例外狀況終止方法。If the key is not found, the exception thrown by the inner Dictionary<TKey,TValue> is allowed to terminate the method. 區塊是用來ExitReadLock執行方法, 以確保呼叫端結束讀取模式。 finallyA finally block is used to execute the ExitReadLock method, ensuring that the caller exits read mode.

此程式碼是針對ReaderWriterLockSlim類別提供的較大範例的一部分。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. TryEnterReadLock使用方法來封鎖指定的間隔, 然後在呼叫執行緒未在該間隔期間進入讀取模式時傳回。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.

如果有一或多個執行緒正在等候進入寫入模式, 則呼叫EnterReadLock方法的執行緒會封鎖, 直到這些執行緒有超時或進入寫入模式, 然後再從中結束為止。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. 如果有其他執行緒正在等候進入可升級模式, 而且沒有線程等候進入寫入模式, 則呼叫EnterReadLock方法的執行緒會立即進入讀取模式, 而不會封鎖。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.

適用於