ReaderWriterLockSlim.TryEnterUpgradeableReadLock 方法

定義

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

多載

TryEnterUpgradeableReadLock(Int32)

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

TryEnterUpgradeableReadLock(TimeSpan)

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

TryEnterUpgradeableReadLock(Int32)

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

public:
 bool TryEnterUpgradeableReadLock(int millisecondsTimeout);
public bool TryEnterUpgradeableReadLock (int millisecondsTimeout);
member this.TryEnterUpgradeableReadLock : int -> bool
Public Function TryEnterUpgradeableReadLock (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 upgradeable 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 upgradeable 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.

備註

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

當執行緒通常會存取ReaderWriterLockSlim以讀取模式保護的資源時, 請使用可升級模式, 但如果符合特定條件, 可能需要進入寫入模式。Use upgradeable mode when a thread usually accesses the resource that is protected by the ReaderWriterLockSlim in read mode, but may need to enter write mode if certain conditions are met. 可升級模式中的執行緒可以升級至寫入模式, 或降級為讀取模式。A thread in upgradeable mode can upgrade to write mode or downgrade to read mode.

在任何指定時間內, 只有一個執行緒可以進入可升級模式的鎖定。Only one thread can enter a lock in upgradeable mode at any given time. 如果執行緒處於可升級模式, 而且沒有任何執行緒等候進入寫入模式, 則任何數目的其他執行緒都可以進入讀取模式, 即使有線程等候進入可升級模式也一樣。If a thread is in upgradeable mode, and there are no threads waiting to enter write mode, any number of other threads can enter read mode, even if there are threads waiting to enter upgradeable mode.

如果有一或多個執行緒正在等候進入寫入模式, 則呼叫TryEnterUpgradeableReadLock方法的執行緒會封鎖, 直到這些執行緒有超時或進入寫入模式, 然後從它結束, 或直到呼叫執行緒本身的逾時間隔到期為止。If one or more threads are waiting to enter write mode, a thread that calls the TryEnterUpgradeableReadLock method blocks until those threads have either timed out or entered write mode and then exited from it, or until the calling thread's own time-out interval expires.

注意

如果鎖定允許遞迴, 已進入可升級模式鎖定的執行緒可以遞迴進入可升級模式, 即使其他執行緒正在等候進入寫入模式也一樣。If a lock allows recursion, a thread that has entered the lock in upgradeable mode can enter upgradeable mode recursively, even if other threads are waiting to enter write mode.

TryEnterUpgradeableReadLock(TimeSpan)

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

public:
 bool TryEnterUpgradeableReadLock(TimeSpan timeout);
public bool TryEnterUpgradeableReadLock (TimeSpan timeout);
member this.TryEnterUpgradeableReadLock : TimeSpan -> bool
Public Function TryEnterUpgradeableReadLock (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 upgradeable 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 upgradeable 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.

當執行緒通常會ReaderWriterLockSlim在讀取模式下存取受保護的資源時, 請使用可升級模式, 但如果符合特定條件, 可能需要進入寫入模式。Use upgradeable mode when a thread usually accesses the resource protected by the ReaderWriterLockSlim in read mode, but may need to enter write mode if certain conditions are met. 可升級模式中的執行緒可以升級至寫入模式, 或降級為讀取模式。A thread in upgradeable mode can upgrade to write mode or downgrade to read mode.

在任何指定時間內, 只有一個執行緒可以進入可升級模式的鎖定。Only one thread can enter a lock in upgradeable mode at any given time. 如果執行緒處於可升級模式, 而且沒有任何執行緒等候進入寫入模式, 則任何數目的其他執行緒都可以進入讀取模式, 即使有線程等候進入可升級模式也一樣。If a thread is in upgradeable mode, and there are no threads waiting to enter write mode, any number of other threads can enter read mode, even if there are threads waiting to enter upgradeable mode.

如果有一或多個執行緒正在等候進入寫入模式, 則呼叫TryEnterUpgradeableReadLock方法的執行緒會封鎖, 直到這些執行緒有超時或進入寫入模式, 然後從它結束, 或直到呼叫執行緒本身的逾時間隔到期為止。If one or more threads are waiting to enter write mode, a thread that calls the TryEnterUpgradeableReadLock method blocks until those threads have either timed out or entered write mode and then exited from it, or until the calling thread's own time-out interval expires.

注意

如果鎖定允許遞迴, 已進入可升級模式鎖定的執行緒可以遞迴進入可升級模式, 即使其他執行緒正在等候進入寫入模式也一樣。If a lock allows recursion, a thread that has entered the lock in upgradeable mode can enter upgradeable mode recursively, even if other threads are waiting to enter write mode.

適用於