ReaderWriterLockSlim.ExitWriteLock 方法

定義

減少寫入模式遞迴的計數,如果得出的計數為 0 (零),則結束寫入模式。Reduces the recursion count for write mode, and exits write mode if the resulting count is 0 (zero).

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

例外狀況

目前的執行緒尚未進入寫入模式的鎖定狀態。The current thread has not entered the lock in write mode.

範例

下列範例顯示如何使用finally區塊來ExitWriteLock執行方法, 以確保呼叫端結束寫入模式。The following example shows how to use a finally block to execute the ExitWriteLock method, ensuring that the caller exits 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. EnterWriteLock方法是用來進入寫入模式的鎖定。The EnterWriteLock method is used to enter the lock in 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 is not sensitive to recursion order. 例如, 如果執行緒進入可升級模式的鎖定, 然後進入寫入模式的鎖定, 執行緒結束這兩種模式的順序並不重要。For example, if a thread enters a lock in upgradeable mode and then enters the lock in write mode, the order in which the thread exits the two modes does not matter. 如果鎖定允許遞迴, 執行緒可以進入寫入模式的鎖定, 然後在讀取模式中以遞迴方式輸入它;執行緒離開讀取模式和寫入模式的順序並不重要。If a lock allows recursion, a thread can enter the lock in write mode and then enter it recursively in read mode; the order in which the thread exits read mode and write mode does not matter.

結束鎖定可能會通知其他等候中的執行緒。Exiting the lock might signal other waiting threads.

適用於