System.Threading.Monitor.TryEnter Method

Attempts, for the specified number of milliseconds, to acquire an exclusive lock on the specified object.

Syntax

public static bool TryEnter (object obj, int millisecondsTimeout)

Parameters

millisecondsTimeout
The number of milliseconds to wait for the lock.
obj
The object on which to acquire the lock.
millisecondsTimeout
The number of milliseconds to wait for the lock.

Returns

true if the current thread acquires the lock; otherwise, false.

Exceptions

TypeReason
ArgumentNullException obj is null.
ArgumentOutOfRangeException millisecondsTimeout is negative, and not equal to Timeout.Infinite .

Remarks

If the millisecondsTimeout parameter equals Timeout.Infinite, this method is equivalent to Monitor.Enter(object). If millisecondsTimeout equals 0, this method is equivalent to Monitor.TryEnter(object).

Note:

Use System.Threading.Monitor to lock objects (that is, reference types), not value types. For details, see Monitor.Enter(object) and the conceptual topic [<topic://cpconMonitor>].

Requirements

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