System.Threading.ReaderWriterLock.UpgradeToWriterLock Method

Upgrades a reader lock to the writer lock, using a TimeSpan value for the time-out.

Syntax

public LockCookie UpgradeToWriterLock (TimeSpan timeout)

Parameters

timeout
The TimeSpan specifying the time-out period.

Returns

A System.Threading.LockCookie value.

Remarks

When a thread calls UpgradeToWriterLock the reader lock is released, regardless of the lock count, and the thread goes to the end of the queue for the writer lock. Thus, other threads might write to the resource before the thread that requested the upgrade is granted the writer lock.

Note:

The time-out exception is not thrown until the thread that called the ReaderWriterLock.UpgradeToWriterLock(int) method can reacquire the reader lock. If there are no other threads waiting for the writer lock, this happens immediately. However, if another thread is queued for the writer lock, the thread that called the ReaderWriterLock.UpgradeToWriterLock(int) method cannot reacquire the reader lock until all current readers have released their locks, and one thread has acquired and released the writer lock. This is true even if the other thread that requested the writer lock requested it after the current thread called the ReaderWriterLock.UpgradeToWriterLock(int) method.

To restore the lock state, call ReaderWriterLock.DowngradeFromWriterLock(LockCookie@) using the System.Threading.LockCookie returned by UpgradeToWriterLock. Do not use this LockCookie with ReaderWriterLock.RestoreLock(LockCookie@).

When a thread has no reader lock, do not use UpgradeToWriterLock. Use ReaderWriterLock.AcquireWriterLock(int) instead.

For valid time-out values, see System.Threading.ReaderWriterLock.

Requirements

Namespace: System.Threading
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0