ReaderWriterLockSlim::TryEnterWriteLock Method (TimeSpan)

 

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

Namespace:   System.Threading
Assembly:  System.Core (in System.Core.dll)

public:
bool TryEnterWriteLock(
	TimeSpan timeout
)

Parameters

timeout
Type: System::TimeSpan

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

Return Value

Type: System::Boolean

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

Exception Condition
LockRecursionException

The RecursionPolicy property is LockRecursionPolicy::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.

ArgumentOutOfRangeException

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.

ObjectDisposedException

The ReaderWriterLockSlim object has been disposed.

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

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.

System_CAPS_noteNote

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.

Universal Windows Platform
Available since 8
.NET Framework
Available since 3.5
Portable Class Library
Supported in: portable .NET platforms
Windows Phone Silverlight
Available since 8.0
Windows Phone
Available since 8.1
Return to top
Show: