System.Threading.Monitor.TryEnter Method

Attempts to acquire an exclusive lock on the specified object.

Syntax

public static bool TryEnter (object obj)

Parameters

obj
The object on which to acquire the lock.

Returns

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

Exceptions

TypeReason
ArgumentNullException obj is null.

Remarks

If successful, this method acquires an exclusive lock on the obj parameter. This method returns immediately, whether or not the lock is available.

This method is similar to Monitor.Enter(object), but it will never block. If the thread cannot enter without blocking, the method returns false, and the thread does not enter the critical section.

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