TryEnterReadLock Method (TimeSpan)
Collapse the table of content
Expand the table of content

ReaderWriterLockSlim.TryEnterReadLock Method (TimeSpan)

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

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

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

public bool TryEnterReadLock(
	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 read mode, otherwise, false.

ExceptionCondition
LockRecursionException

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

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.

Caution noteCaution:

Do not use ReaderWriterLockSlim. It throws a MethodAccessException exception when two readers enter the lock. In most cases, use the Monitor class instead.

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.

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.

NoteNote:

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

Windows Phone OS

Supported in: 8.1, 8.0

Show:
© 2017 Microsoft