Mono Class Library: System.Threading.Monitor Overview | Members

System.Threading.Monitor.Wait Method

Releases the lock on an object and blocks the current thread until it reacquires the lock. [Edit]

public static bool Wait (object obj)

Parameters

obj
The object on which to wait. [Edit]

Returns

true if the call returned because the caller reacquired the lock for the specified object. This method does not return if the lock is not reacquired. [Edit]

Exceptions

TypeReason
ArgumentNullExceptionobj is null. [Edit]
System.Threading.SynchronizationLockExceptionThe calling thread does not own the lock for the specified object. [Edit]

Remarks

This method reacquires an exclusive lock on obj.

The thread that currently owns the lock on the specified object invokes this method in order to release the object so that another thread can access it. The caller is blocked while waiting to reacquire the lock. This method is called when the caller is waiting for a change in the state of the object, which occurs as a result of another thread's operations on the object.

When a thread calls Wait, it releases the lock on the object and enters the object's waiting queue. The next thread in the object's ready queue (if there is one) acquires the lock and has exclusive use of the object. All threads that call Wait remain in the waiting queue until they receive a signal via Monitor.Pulse(object) or Monitor.PulseAll(object) sent by the owner of the lock. If Pulse is sent, only the thread at the head of the waiting queue is affected. If PulseAll is sent, all threads that are waiting for the object are affected. When the signal is received, one or more threads leave the waiting queue and enter the ready queue. A thread in the ready queue is permitted to reacquire the lock.

This method returns when the calling thread reacquires the lock on the object. Note that this method blocks indefinitely if the holder of the lock does not call Monitor.Pulse(object) or Monitor.PulseAll(object).

The caller executes Monitor.Wait(object, int, bool) once, regardless of the number of times Monitor.Enter(object) has been invoked for the specified object. Conceptually, the Monitor.Wait(object, int, bool) method stores the number of times the caller invoked Monitor.Enter(object) on the object and invokes Monitor.Exit(object) as many times as necessary to fully release the locked object. The caller then blocks while waiting to reacquire the object. When the caller reacquires the lock, the system calls Monitor.Enter(object) as many times as necessary to restore the saved Enter count for the caller.

Calling Monitor.Wait(object, int, bool) releases the lock for the specified object only; if the caller is the owner of locks on other objects, these locks are not released.

[Edit]

Requirements

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