Редагувати

Поділитися через


ReaderWriterLockSlim.TryEnterUpgradeableReadLock Method

Definition

Tries to enter the lock in upgradeable mode, with an optional time-out.

Overloads

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)

Source:
ReaderWriterLockSlim.cs
Source:
ReaderWriterLockSlim.cs
Source:
ReaderWriterLockSlim.cs

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

Parameters

millisecondsTimeout
Int32

The number of milliseconds to wait, or -1 (Infinite) to wait indefinitely.

Returns

true if the calling thread entered upgradeable mode, otherwise, false.

Exceptions

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.

The value of millisecondsTimeout is negative, but it is not equal to Infinite (-1), which is the only negative value allowed.

The ReaderWriterLockSlim object has been disposed.

Remarks

If millisecondsTimeout is 0 (zero), this method checks the lock state and returns false immediately if the desired state is unavailable.

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.

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.

Note

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.

Applies to

TryEnterUpgradeableReadLock(TimeSpan)

Source:
ReaderWriterLockSlim.cs
Source:
ReaderWriterLockSlim.cs
Source:
ReaderWriterLockSlim.cs

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

Parameters

timeout
TimeSpan

The interval to wait, or -1 milliseconds to wait indefinitely.

Returns

true if the calling thread entered upgradeable mode, otherwise, false.

Exceptions

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.

The value of timeout is negative, but it is not equal to -1 milliseconds, which is the only negative value allowed.

-or-

The value of timeout is greater than Int32.MaxValue milliseconds.

The ReaderWriterLockSlim object has been disposed.

Remarks

If timeout is 0 (zero), this method checks the lock state and returns false immediately if the desired state is unavailable.

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.

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.

Note

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.

Applies to