ReaderWriterLockSlim.TryEnterReadLock 方法

定義

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

多載

TryEnterReadLock(Int32)

嘗試以選用的整數逾時,進入讀取模式的鎖定狀態。Tries to enter the lock in read mode, with an optional integer time-out.

TryEnterReadLock(TimeSpan)

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

TryEnterReadLock(Int32)

嘗試以選用的整數逾時,進入讀取模式的鎖定狀態。Tries to enter the lock in read mode, with an optional integer time-out.

public:
 bool TryEnterReadLock(int millisecondsTimeout);
public bool TryEnterReadLock (int millisecondsTimeout);
member this.TryEnterReadLock : int -> bool
Public Function TryEnterReadLock (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 read mode, otherwise, false.

例外狀況

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

-或--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.

多個執行緒可以同時進入讀取模式。Multiple threads can enter read mode at the same time.

如果有一或多個執行緒正在等候進入寫入模式, 則呼叫TryEnterReadLock方法的執行緒會封鎖, 直到這些執行緒有超時或進入寫入模式, 然後從它結束, 或直到呼叫執行緒本身的逾時間隔到期為止。If one or more threads are waiting to enter write mode, a thread that calls the TryEnterReadLock 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 read mode can enter read mode recursively, even if other threads are waiting to enter write mode.

當其他執行緒處於讀取模式時, 一個執行緒可以處於可升級模式。One thread can be in upgradeable mode while other threads are in read mode. 如果有其他執行緒正在等候進入可升級模式, 而且沒有線程等候進入寫入模式, 則呼叫TryEnterReadLock方法的執行緒會立即進入讀取模式, 而不會封鎖。If additional threads are waiting to enter upgradeable mode, and there are no threads waiting to enter write mode, threads that call the TryEnterReadLock method enter read mode immediately and do not block.

TryEnterReadLock(TimeSpan)

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

public:
 bool TryEnterReadLock(TimeSpan timeout);
public bool TryEnterReadLock (TimeSpan timeout);
member this.TryEnterReadLock : TimeSpan -> bool
Public Function TryEnterReadLock (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 read mode, otherwise, false.

例外狀況

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

-或--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.

多個執行緒可以同時進入讀取模式的鎖定。Multiple threads can enter the lock in read mode at the same time.

如果有一或多個執行緒排入佇列以進入寫入模式, 則呼叫TryEnterReadLock方法的執行緒會封鎖, 直到這些執行緒有超時或進入寫入模式, 然後從它結束, 或直到呼叫執行緒本身的逾時間隔到期為止。If one or more threads are queued to enter write mode, a thread that calls the TryEnterReadLock 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 read mode can enter read mode recursively, even if other threads are waiting to enter write mode.

當其他執行緒處於讀取模式時, 一個執行緒可以處於可升級模式。One thread can be in upgradeable mode while other threads are in read mode. 如果有其他執行緒正在等候進入可升級模式, 而且沒有線程等候進入寫入模式, 則呼叫TryEnterReadLock方法的執行緒會立即進入讀取模式, 而不會封鎖。If additional threads are waiting to enter upgradeable mode, and there are no threads waiting to enter write mode, threads that call the TryEnterReadLock method enter read mode immediately and do not block.

適用於