ReaderWriterLockSlim.TryEnterWriteLock 方法

定義

嘗試以選用的逾時,在寫入模式下進入鎖定狀態。Tries to enter the lock in write mode, with an optional time-out.

多載

TryEnterWriteLock(Int32)

嘗試以選用的逾時,在寫入模式下進入鎖定狀態。Tries to enter the lock in write mode, with an optional time-out.

TryEnterWriteLock(TimeSpan)

嘗試以選用的逾時,在寫入模式下進入鎖定狀態。Tries to enter the lock in write mode, with an optional time-out.

TryEnterWriteLock(Int32)

嘗試以選用的逾時,在寫入模式下進入鎖定狀態。Tries to enter the lock in write mode, with an optional time-out.

public:
 bool TryEnterWriteLock(int millisecondsTimeout);
public bool TryEnterWriteLock (int millisecondsTimeout);
member this.TryEnterWriteLock : int -> bool
Public Function TryEnterWriteLock (millisecondsTimeout As Integer) As Boolean

參數

millisecondsTimeout
Int32

要等待的毫秒數,或 -1 (Infinite) 則為無限期等待。The number of milliseconds to wait, or -1 (Infinite) to wait indefinitely.

傳回

如果呼叫的執行緒已進入寫入模式,則為 true,否則為 falsetrue if the calling thread entered write mode, otherwise, false.

例外狀況

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

-或--or- 目前執行緒一開始已進入讀取模式的鎖定狀態,因此嘗試進入寫入模式可能會造成死結。The current thread initially entered the lock in read mode, and therefore trying to enter 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.

millisecondsTimeout 的值為負,但不等於 Infinite (-1),這是唯一允許的負值。The value of millisecondsTimeout is negative, but it is not equal to Infinite (-1), which is the only negative value allowed.

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

範例

下列範例顯示如何使用TryEnterWriteLock方法, 以超時的方式進入寫入模式的鎖定。範例中顯示的方法會將新的索引鍵/值組加入至已同步處理的快取。The following example shows how to use the TryEnterWriteLock method to enter the lock in write mode, with a time-out. The method shown in the example adds a new key/value pair to the synchronized cache. 如果線上程進入鎖定之前, 經過指定的逾時間隔, 則方法false會傳回。If the specified time-out interval elapses before the thread enters the lock, the method returns false. true如果加入索引鍵/值組, 此方法會傳回。The method returns true if the key/value pair is added.

如果索引鍵已經在快取中, 則會允許內部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 the lock.

此程式碼是針對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 bool AddWithTimeout(int key, string value, int timeout)
{
    if (cacheLock.TryEnterWriteLock(timeout))
    {
        try
        {
            innerCache.Add(key, value);
        }
        finally
        {
            cacheLock.ExitWriteLock();
        }
        return true;
    }
    else
    {
        return false;
    }
}
Public Function AddWithTimeout(ByVal key As Integer, ByVal value As String, _
                               ByVal timeout As Integer) As Boolean
    If cacheLock.TryEnterWriteLock(timeout) Then
        Try
            innerCache.Add(key, value)
        Finally
            cacheLock.ExitWriteLock()
        End Try
        Return True
    Else
        Return False
    End If
End Function

備註

如果millisecondsTimeout為 0 (零), 這個方法會檢查鎖定狀態false , 並在需要的狀態無法使用時立即傳回。If millisecondsTimeout is 0 (zero), this method checks the lock state and returns false immediately if the desired state is unavailable.

如果其他執行緒在讀取模式中進入鎖定, 則呼叫TryEnterWriteLock方法的執行緒會封鎖, 直到這些執行緒結束讀取模式, 或直到經過逾時間隔為止。If other threads have entered the lock in read mode, a thread that calls the TryEnterWriteLock method blocks until those threads have exited read mode or until the time-out interval has elapsed. 當執行緒被封鎖而無法進入寫入模式時, 會嘗試進入讀取模式或可升級模式區塊的其他執行緒, 直到等候進入寫入模式的所有線程都已超時或進入寫入模式, 然後再從中結束為止。While threads are blocked 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.

TryEnterWriteLock(TimeSpan)

嘗試以選用的逾時,在寫入模式下進入鎖定狀態。Tries to enter the lock in write mode, with an optional time-out.

public:
 bool TryEnterWriteLock(TimeSpan timeout);
public bool TryEnterWriteLock (TimeSpan timeout);
member this.TryEnterWriteLock : TimeSpan -> bool
Public Function TryEnterWriteLock (timeout As TimeSpan) As Boolean

參數

timeout
TimeSpan

等待的間隔,或 -1 毫秒無限期等待。The interval to wait, or -1 milliseconds to wait indefinitely.

傳回

如果呼叫的執行緒已進入寫入模式,則為 true,否則為 falsetrue if the calling thread entered write mode, otherwise, false.

例外狀況

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

-或--or- 目前執行緒一開始已進入讀取模式的鎖定狀態,因此嘗試進入寫入模式可能會造成死結。The current thread initially entered the lock in read mode, and therefore trying to enter 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.

timeout 的值為負,但不等於 -1 毫秒,這是唯一允許的負值。The value of timeout is negative, but it is not equal to -1 milliseconds, which is the only negative value allowed.

-或--or- timeout 的值大於 MaxValue 毫秒。The value of timeout is greater than MaxValue milliseconds.

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

備註

如果timeout為 0 (零), 這個方法會檢查鎖定狀態false , 並在需要的狀態無法使用時立即傳回。If timeout is 0 (zero), this method checks the lock state and returns false immediately if the desired state is unavailable.

如果其他執行緒在讀取模式中進入鎖定, 則呼叫TryEnterWriteLock方法的執行緒會封鎖, 直到這些執行緒結束讀取模式, 或直到經過逾時間隔為止。If other threads have entered the lock in read mode, a thread that calls the TryEnterWriteLock method blocks until those threads have exited read mode or until the time-out interval has elapsed. 當執行緒被封鎖而無法進入寫入模式時, 會嘗試進入讀取模式或可升級模式區塊的其他執行緒, 直到等候進入寫入模式的所有線程都已超時或進入寫入模式, 然後再從中結束為止。While threads are blocked 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.

適用於