ReaderWriterLockSlim.EnterWriteLock 方法

定義

嘗試進入寫入模式的鎖定。Tries to enter the lock in write mode.

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

例外狀況

RecursionPolicy 屬性為 NoRecursion,且目前的執行緒已進入任何模式的鎖定狀態。The RecursionPolicy property is NoRecursion and the current thread has already entered the lock in any mode.

-或--or- 目前的執行緒已進入真實模式,而且還沒有擁有寫入鎖定,因此嘗試進入寫入模式鎖定可能會造成死結。The current thread has entered read mode and doesn't already own a write lock, 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.

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

範例

下列範例示範如何使用EnterWriteLock方法, 進入寫入模式的鎖定。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. 如果索引鍵已經在快取中, 則會允許內部Dictionary<TKey,TValue>擲回例外狀況來終止方法。If the key is already in the cache, the exception thrown by the inner Dictionary<TKey,TValue> is allowed to terminate the method. 區塊是用來ExitWriteLock執行方法, 以確保呼叫端結束寫入模式。 finallyA finally block is used to execute the ExitWriteLock method, ensuring that the caller exits write 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 void Add(int key, string value)
{
    cacheLock.EnterWriteLock();
    try
    {
        innerCache.Add(key, value);
    }
    finally
    {
        cacheLock.ExitWriteLock();
    }
}
Public Sub Add(ByVal key As Integer, ByVal value As String)
    cacheLock.EnterWriteLock()
    Try
        innerCache.Add(key, value)
    Finally
        cacheLock.ExitWriteLock()
    End Try
End Sub

備註

這個方法會封鎖, 直到呼叫執行緒進入鎖定為止, 因此可能永遠不會傳回。This method blocks until the calling thread enters the lock, and therefore might never return. TryEnterWriteLock使用方法來封鎖指定的間隔, 然後在呼叫執行緒未在該間隔期間進入寫入模式時傳回。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.

如果其他執行緒在讀取模式中進入鎖定, 則呼叫EnterWriteLock方法的執行緒會封鎖, 直到這些執行緒結束讀取模式為止。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.

注意

如果鎖定允許遞迴, 已進入寫入模式鎖定的執行緒可以遞迴方式進入寫入模式, 即使其他執行緒正在等候進入寫入模式也一樣。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.

適用於