Monitor.Wait Method

Definition

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

Overloads

Wait(Object, Int32, Boolean)

Releases the lock on an object and blocks the current thread until it reacquires the lock. If the specified time-out interval elapses, the thread enters the ready queue. This method also specifies whether the synchronization domain for the context (if in a synchronized context) is exited before the wait and reacquired afterward.

Wait(Object)

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

Wait(Object, Int32)

Releases the lock on an object and blocks the current thread until it reacquires the lock. If the specified time-out interval elapses, the thread enters the ready queue.

Wait(Object, TimeSpan)

Releases the lock on an object and blocks the current thread until it reacquires the lock. If the specified time-out interval elapses, the thread enters the ready queue.

Wait(Object, TimeSpan, Boolean)

Releases the lock on an object and blocks the current thread until it reacquires the lock. If the specified time-out interval elapses, the thread enters the ready queue. Optionally exits the synchronization domain for the synchronized context before the wait and reacquires the domain afterward.

Wait(Object, Int32, Boolean)

Releases the lock on an object and blocks the current thread until it reacquires the lock. If the specified time-out interval elapses, the thread enters the ready queue. This method also specifies whether the synchronization domain for the context (if in a synchronized context) is exited before the wait and reacquired afterward.

public:
 static bool Wait(System::Object ^ obj, int millisecondsTimeout, bool exitContext);
public static bool Wait (object obj, int millisecondsTimeout, bool exitContext);
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public static bool Wait (object obj, int millisecondsTimeout, bool exitContext);
static member Wait : obj * int * bool -> bool
[<System.Runtime.Versioning.UnsupportedOSPlatform("browser")>]
static member Wait : obj * int * bool -> bool
Public Shared Function Wait (obj As Object, millisecondsTimeout As Integer, exitContext As Boolean) As Boolean

Parameters

obj
Object

The object on which to wait.

millisecondsTimeout
Int32

The number of milliseconds to wait before the thread enters the ready queue.

exitContext
Boolean

true to exit and reacquire the synchronization domain for the context (if in a synchronized context) before the wait; otherwise, false.

Returns

true if the lock was reacquired before the specified time elapsed; false if the lock was reacquired after the specified time elapsed. The method does not return until the lock is reacquired.

Attributes

Exceptions

The obj parameter is null.

Wait is not invoked from within a synchronized block of code.

The thread that invokes Wait is later interrupted from the waiting state. This happens when another thread calls this thread's Interrupt() method.

The value of the millisecondsTimeout parameter is negative, and is not equal to Infinite.

Remarks

For more information about this API, see Supplemental API remarks for Monitor.Wait.

See also

Applies to

Wait(Object)

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

public:
 static bool Wait(System::Object ^ obj);
public static bool Wait (object obj);
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public static bool Wait (object obj);
static member Wait : obj -> bool
[<System.Runtime.Versioning.UnsupportedOSPlatform("browser")>]
static member Wait : obj -> bool
Public Shared Function Wait (obj As Object) As Boolean

Parameters

obj
Object

The object on which to wait.

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.

Attributes

Exceptions

The obj parameter is null.

The calling thread does not own the lock for the specified object.

The thread that invokes Wait is later interrupted from the waiting state. This happens when another thread calls this thread's Interrupt() method.

Remarks

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 needs to wait for a state change that will occur as a result of another thread's operations.

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 from Pulse or PulseAll, 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 Pulse or PulseAll.

The caller executes Wait once, regardless of the number of times Enter has been invoked for the specified object. Conceptually, the Wait method stores the number of times the caller invoked Enter on the object and invokes Exit 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 Enter as many times as necessary to restore the saved Enter count for the caller. Calling Wait releases the lock for the specified object only; if the caller is the owner of locks on other objects, these locks are not released.

Note that a synchronized object holds several references, including a reference to the thread that currently holds the lock, a reference to the ready queue, which contains the threads that are ready to obtain the lock, and a reference to the waiting queue, which contains the threads that are waiting for notification of a change in the object's state.

The Pulse, PulseAll, and Wait methods must be invoked from within a synchronized block of code.

The remarks for the Pulse method explain what happens if Pulse is called when no threads are waiting.

See also

Applies to

Wait(Object, Int32)

Releases the lock on an object and blocks the current thread until it reacquires the lock. If the specified time-out interval elapses, the thread enters the ready queue.

public:
 static bool Wait(System::Object ^ obj, int millisecondsTimeout);
public static bool Wait (object obj, int millisecondsTimeout);
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public static bool Wait (object obj, int millisecondsTimeout);
static member Wait : obj * int -> bool
[<System.Runtime.Versioning.UnsupportedOSPlatform("browser")>]
static member Wait : obj * int -> bool
Public Shared Function Wait (obj As Object, millisecondsTimeout As Integer) As Boolean

Parameters

obj
Object

The object on which to wait.

millisecondsTimeout
Int32

The number of milliseconds to wait before the thread enters the ready queue.

Returns

true if the lock was reacquired before the specified time elapsed; false if the lock was reacquired after the specified time elapsed. The method does not return until the lock is reacquired.

Attributes

Exceptions

The obj parameter is null.

The calling thread does not own the lock for the specified object.

The thread that invokes Wait is later interrupted from the waiting state. This happens when another thread calls this thread's Interrupt() method.

The value of the millisecondsTimeout parameter is negative, and is not equal to Infinite.

Remarks

This method does not return until it reacquires an exclusive lock on the obj parameter.

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 needs to wait for a state change that will occur as a result of another thread's operations.

The time-out ensures that the current thread does not block indefinitely if another thread releases the lock without first calling the Pulse or PulseAll method. It also moves the thread to the ready queue, bypassing other threads ahead of it in the wait queue, so that it can reacquire the lock sooner. The thread can test the return value of the Wait method to determine whether it reacquired the lock prior to the time-out. The thread can evaluate the conditions that caused it to enter the wait, and if necessary call the Wait method again.

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. The thread that invoked Wait remains in the waiting queue until either a thread that holds the lock invokes PulseAll, or it is the next in the queue and a thread that holds the lock invokes Pulse. However, if millisecondsTimeout elapses before another thread invokes this object's Pulse or PulseAll method, the original thread is moved to the ready queue in order to regain the lock.

Note

If Infinite is specified for the millisecondsTimeout parameter, this method blocks indefinitely unless the holder of the lock calls Pulse or PulseAll. If millisecondsTimeout equals 0, the thread that calls Wait releases the lock and then immediately enters the ready queue in order to regain the lock.

The caller executes Wait once, regardless of the number of times Enter has been invoked for the specified object. Conceptually, the Wait method stores the number of times the caller invoked Enter on the object and invokes Exit 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 Enter as many times as necessary to restore the saved Enter count for the caller. Calling Wait releases the lock for the specified object only; if the caller is the owner of locks on other objects, these locks are not released.

Note

A synchronized object holds several references, including a reference to the thread that currently holds the lock, a reference to the ready queue, which contains the threads that are ready to obtain the lock, and a reference to the waiting queue, which contains the threads that are waiting for notification of a change in the object's state.

The Pulse, PulseAll, and Wait methods must be invoked from within a synchronized block of code.

The remarks for the Pulse method explain what happens if Pulse is called when no threads are waiting.

See also

Applies to

Wait(Object, TimeSpan)

Releases the lock on an object and blocks the current thread until it reacquires the lock. If the specified time-out interval elapses, the thread enters the ready queue.

public:
 static bool Wait(System::Object ^ obj, TimeSpan timeout);
public static bool Wait (object obj, TimeSpan timeout);
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public static bool Wait (object obj, TimeSpan timeout);
static member Wait : obj * TimeSpan -> bool
[<System.Runtime.Versioning.UnsupportedOSPlatform("browser")>]
static member Wait : obj * TimeSpan -> bool
Public Shared Function Wait (obj As Object, timeout As TimeSpan) As Boolean

Parameters

obj
Object

The object on which to wait.

timeout
TimeSpan

A TimeSpan representing the amount of time to wait before the thread enters the ready queue.

Returns

true if the lock was reacquired before the specified time elapsed; false if the lock was reacquired after the specified time elapsed. The method does not return until the lock is reacquired.

Attributes

Exceptions

The obj parameter is null.

The calling thread does not own the lock for the specified object.

The thread that invokes Wait is later interrupted from the waiting state. This happens when another thread calls this thread's Interrupt() method.

The value of the timeout parameter in milliseconds is negative and does not represent Infinite (-1 millisecond), or is greater than Int32.MaxValue.

Remarks

This method does not return until it reacquires an exclusive lock on the obj parameter.

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 needs to wait for a state change that will occur as a result of another thread's operations.

The time-out ensures that the current thread does not block indefinitely if another thread releases the lock without first calling the Pulse or PulseAll method. It also moves the thread to the ready queue, bypassing other threads ahead of it in the wait queue, so that it can reacquire the lock sooner. The thread can test the return value of the Wait method to determine whether it reacquired the lock prior to the time-out. The thread can evaluate the conditions that caused it to enter the wait, and if necessary call the Wait method again.

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. The thread that invoked Wait remains in the waiting queue until either a thread that holds the lock invokes PulseAll, or it is the next in the queue and a thread that holds the lock invokes Pulse. However, if timeout elapses before another thread invokes this object's Pulse or PulseAll method, the original thread is moved to the ready queue in order to regain the lock.

Note

If a TimeSpan representing -1 millisecond is specified for the timeout parameter, this method blocks indefinitely unless the holder of the lock calls Pulse or PulseAll. If timeout is 0 milliseconds, the thread that calls Wait releases the lock and then immediately enters the ready queue in order to regain the lock.

The caller executes Wait once, regardless of the number of times Enter has been invoked for the specified object. Conceptually, the Wait method stores the number of times the caller invoked Enter on the object and invokes Exit 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 Enter as many times as necessary to restore the saved Enter count for the caller. Calling Wait releases the lock for the specified object only; if the caller is the owner of locks on other objects, these locks are not released.

Note

A synchronized object holds several references, including a reference to the thread that currently holds the lock, a reference to the ready queue, which contains the threads that are ready to obtain the lock, and a reference to the waiting queue, which contains the threads that are waiting for notification of a change in the object's state.

The Pulse, PulseAll, and Wait methods must be invoked from within a synchronized block of code.

The remarks for the Pulse method explain what happens if Pulse is called when no threads are waiting.

See also

Applies to

Wait(Object, TimeSpan, Boolean)

Releases the lock on an object and blocks the current thread until it reacquires the lock. If the specified time-out interval elapses, the thread enters the ready queue. Optionally exits the synchronization domain for the synchronized context before the wait and reacquires the domain afterward.

public:
 static bool Wait(System::Object ^ obj, TimeSpan timeout, bool exitContext);
public static bool Wait (object obj, TimeSpan timeout, bool exitContext);
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public static bool Wait (object obj, TimeSpan timeout, bool exitContext);
static member Wait : obj * TimeSpan * bool -> bool
[<System.Runtime.Versioning.UnsupportedOSPlatform("browser")>]
static member Wait : obj * TimeSpan * bool -> bool
Public Shared Function Wait (obj As Object, timeout As TimeSpan, exitContext As Boolean) As Boolean

Parameters

obj
Object

The object on which to wait.

timeout
TimeSpan

A TimeSpan representing the amount of time to wait before the thread enters the ready queue.

exitContext
Boolean

true to exit and reacquire the synchronization domain for the context (if in a synchronized context) before the wait; otherwise, false.

Returns

true if the lock was reacquired before the specified time elapsed; false if the lock was reacquired after the specified time elapsed. The method does not return until the lock is reacquired.

Attributes

Exceptions

The obj parameter is null.

Wait is not invoked from within a synchronized block of code.

The thread that invokes Wait is later interrupted from the waiting state. This happens when another thread calls this thread's Interrupt() method.

The timeout parameter is negative and does not represent Infinite (-1 millisecond), or is greater than Int32.MaxValue.

Remarks

For more information about this API, see Supplemental API remarks for Monitor.Wait.

See also

Applies to