mscorlib(4.0.0.0) API with additions
System.Threading.Monitor Class Reference

Provides a mechanism that synchronizes access to objects. More...

Public Member Functions

static void Enter (object obj)
 Acquires an exclusive lock on the specified object. More...
 
static void Exit (object obj)
 Releases an exclusive lock on the specified object. More...
 

Static Public Member Functions

static void Enter (object obj, ref bool lockTaken)
 Acquires an exclusive lock on the specified object, and atomically sets a value that indicates whether the lock was taken. More...
 
static bool TryEnter (object obj)
 Attempts to acquire an exclusive lock on the specified object. More...
 
static void TryEnter (object obj, ref bool lockTaken)
 Attempts to acquire an exclusive lock on the specified object, and atomically sets a value that indicates whether the lock was taken. More...
 
static bool TryEnter (object obj, int millisecondsTimeout)
 Attempts, for the specified number of milliseconds, to acquire an exclusive lock on the specified object. More...
 
static bool TryEnter (object obj, TimeSpan timeout)
 Attempts, for the specified amount of time, to acquire an exclusive lock on the specified object. More...
 
static void TryEnter (object obj, int millisecondsTimeout, ref bool lockTaken)
 Attempts, for the specified number of milliseconds, to acquire an exclusive lock on the specified object, and atomically sets a value that indicates whether the lock was taken. More...
 
static void TryEnter (object obj, TimeSpan timeout, ref bool lockTaken)
 Attempts, for the specified amount of time, to acquire an exclusive lock on the specified object, and atomically sets a value that indicates whether the lock was taken. More...
 
static bool IsEntered (object obj)
 Determines whether the current thread holds the lock on the specified object. More...
 
static bool Wait (object obj, int millisecondsTimeout, bool exitContext)
 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. More...
 
static bool Wait (object obj, TimeSpan timeout, bool exitContext)
 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. More...
 
static bool Wait (object obj, int millisecondsTimeout)
 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. More...
 
static bool Wait (object obj, TimeSpan timeout)
 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. More...
 
static bool Wait (object obj)
 Releases the lock on an object and blocks the current thread until it reacquires the lock. More...
 
static void Pulse (object obj)
 Notifies a thread in the waiting queue of a change in the locked object's state. More...
 
static void PulseAll (object obj)
 Notifies all waiting threads of a change in the object's state. More...
 

Detailed Description

Provides a mechanism that synchronizes access to objects.

Definition at line 13 of file Monitor.cs.

Member Function Documentation

◆ Enter() [1/2]

static void System.Threading.Monitor.Enter ( object  obj)

Acquires an exclusive lock on the specified object.

Parameters
objThe object on which to acquire the monitor lock.
Exceptions
T:System.ArgumentNullExceptionThe obj parameter is null.

◆ Enter() [2/2]

static void System.Threading.Monitor.Enter ( object  obj,
ref bool  lockTaken 
)
static

Acquires an exclusive lock on the specified object, and atomically sets a value that indicates whether the lock was taken.

Parameters
objThe object on which to wait.
lockTakenThe result of the attempt to acquire the lock, passed by reference. The input must be false. The output is true if the lock is acquired; otherwise, the output is false. The output is set even if an exception occurs during the attempt to acquire the lock. Note   If no exception occurs, the output of this method is always true.
Exceptions
T:System.ArgumentExceptionThe input to lockTaken is true.
T:System.ArgumentNullExceptionThe obj parameter is null.

Definition at line 29 of file Monitor.cs.

◆ Exit()

static void System.Threading.Monitor.Exit ( object  obj)

Releases an exclusive lock on the specified object.

Parameters
objThe object on which to release the lock.
Exceptions
T:System.ArgumentNullExceptionThe obj parameter is null.
T:System.Threading.SynchronizationLockExceptionThe current thread does not own the lock for the specified object.

◆ IsEntered()

static bool System.Threading.Monitor.IsEntered ( object  obj)
static

Determines whether the current thread holds the lock on the specified object.

Parameters
objThe object to test.
Returns
true if the current thread holds the lock on obj ; otherwise, false.
Exceptions
T:System.ArgumentNullExceptionobj is null.

Definition at line 171 of file Monitor.cs.

◆ Pulse()

static void System.Threading.Monitor.Pulse ( object  obj)
static

Notifies a thread in the waiting queue of a change in the locked object's state.

Parameters
objThe object a thread is waiting for.
Exceptions
T:System.ArgumentNullExceptionThe obj parameter is null.
T:System.Threading.SynchronizationLockExceptionThe calling thread does not own the lock for the specified object.

Definition at line 280 of file Monitor.cs.

◆ PulseAll()

static void System.Threading.Monitor.PulseAll ( object  obj)
static

Notifies all waiting threads of a change in the object's state.

Parameters
objThe object that sends the pulse.
Exceptions
T:System.ArgumentNullExceptionThe obj parameter is null.
T:System.Threading.SynchronizationLockExceptionThe calling thread does not own the lock for the specified object.

Definition at line 299 of file Monitor.cs.

◆ TryEnter() [1/6]

static bool System.Threading.Monitor.TryEnter ( object  obj)
static

Attempts to acquire an exclusive lock on the specified object.

Parameters
objThe object on which to acquire the lock.
Returns
true if the current thread acquires the lock; otherwise, false.
Exceptions
T:System.ArgumentNullExceptionThe obj parameter is null.

Definition at line 63 of file Monitor.cs.

◆ TryEnter() [2/6]

static void System.Threading.Monitor.TryEnter ( object  obj,
ref bool  lockTaken 
)
static

Attempts to acquire an exclusive lock on the specified object, and atomically sets a value that indicates whether the lock was taken.

Parameters
objThe object on which to acquire the lock.
lockTakenThe result of the attempt to acquire the lock, passed by reference. The input must be false. The output is true if the lock is acquired; otherwise, the output is false. The output is set even if an exception occurs during the attempt to acquire the lock.
Exceptions
T:System.ArgumentExceptionThe input to lockTaken is true.
T:System.ArgumentNullExceptionThe obj parameter is null.

Definition at line 76 of file Monitor.cs.

◆ TryEnter() [3/6]

static bool System.Threading.Monitor.TryEnter ( object  obj,
int  millisecondsTimeout 
)
static

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

Parameters
objThe object on which to acquire the lock.
millisecondsTimeoutThe number of milliseconds to wait for the lock.
Returns
true if the current thread acquires the lock; otherwise, false.
Exceptions
T:System.ArgumentNullExceptionThe obj parameter is null.
T:System.ArgumentOutOfRangeExceptionmillisecondsTimeout is negative, and not equal to F:System.Threading.Timeout.Infinite.

Definition at line 94 of file Monitor.cs.

◆ TryEnter() [4/6]

static bool System.Threading.Monitor.TryEnter ( object  obj,
TimeSpan  timeout 
)
static

Attempts, for the specified amount of time, to acquire an exclusive lock on the specified object.

Parameters
objThe object on which to acquire the lock.
timeoutA T:System.TimeSpan representing the amount of time to wait for the lock. A value of –1 millisecond specifies an infinite wait.
Returns
true if the current thread acquires the lock; otherwise, false.
Exceptions
T:System.ArgumentNullExceptionThe obj parameter is null.
T:System.ArgumentOutOfRangeExceptionThe value of timeout in milliseconds is negative and is not equal to F:System.Threading.Timeout.Infinite (–1 millisecond), or is greater than F:System.Int32.MaxValue.

Definition at line 119 of file Monitor.cs.

◆ TryEnter() [5/6]

static void System.Threading.Monitor.TryEnter ( object  obj,
int  millisecondsTimeout,
ref bool  lockTaken 
)
static

Attempts, for the specified number of milliseconds, to acquire an exclusive lock on the specified object, and atomically sets a value that indicates whether the lock was taken.

Parameters
objThe object on which to acquire the lock.
millisecondsTimeoutThe number of milliseconds to wait for the lock.
lockTakenThe result of the attempt to acquire the lock, passed by reference. The input must be false. The output is true if the lock is acquired; otherwise, the output is false. The output is set even if an exception occurs during the attempt to acquire the lock.
Exceptions
T:System.ArgumentExceptionThe input to lockTaken is true.
T:System.ArgumentNullExceptionThe obj parameter is null.
T:System.ArgumentOutOfRangeExceptionmillisecondsTimeout is negative, and not equal to F:System.Threading.Timeout.Infinite.

Definition at line 133 of file Monitor.cs.

◆ TryEnter() [6/6]

static void System.Threading.Monitor.TryEnter ( object  obj,
TimeSpan  timeout,
ref bool  lockTaken 
)
static

Attempts, for the specified amount of time, to acquire an exclusive lock on the specified object, and atomically sets a value that indicates whether the lock was taken.

Parameters
objThe object on which to acquire the lock.
timeoutThe amount of time to wait for the lock. A value of –1 millisecond specifies an infinite wait.
lockTakenThe result of the attempt to acquire the lock, passed by reference. The input must be false. The output is true if the lock is acquired; otherwise, the output is false. The output is set even if an exception occurs during the attempt to acquire the lock.
Exceptions
T:System.ArgumentExceptionThe input to lockTaken is true.
T:System.ArgumentNullExceptionThe obj parameter is null.
T:System.ArgumentOutOfRangeExceptionThe value of timeout in milliseconds is negative and is not equal to F:System.Threading.Timeout.Infinite (–1 millisecond), or is greater than F:System.Int32.MaxValue.

Definition at line 150 of file Monitor.cs.

◆ Wait() [1/5]

static bool System.Threading.Monitor.Wait ( object  obj,
int  millisecondsTimeout,
bool  exitContext 
)
static

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.

Parameters
objThe object on which to wait.
millisecondsTimeoutThe number of milliseconds to wait before the thread enters the ready queue.
exitContexttrue 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.
Exceptions
T:System.ArgumentNullExceptionThe obj parameter is null.
T:System.Threading.SynchronizationLockExceptionWait is not invoked from within a synchronized block of code.
T:System.Threading.ThreadInterruptedExceptionThe thread that invokes Wait is later interrupted from the waiting state. This happens when another thread calls this thread's M:System.Threading.Thread.Interrupt method.
T:System.ArgumentOutOfRangeExceptionThe value of the millisecondsTimeout parameter is negative, and is not equal to F:System.Threading.Timeout.Infinite.

Definition at line 201 of file Monitor.cs.

◆ Wait() [2/5]

static bool System.Threading.Monitor.Wait ( object  obj,
TimeSpan  timeout,
bool  exitContext 
)
static

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.

Parameters
objThe object on which to wait.
timeoutA T:System.TimeSpan representing the amount of time to wait before the thread enters the ready queue.
exitContexttrue 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.
Exceptions
T:System.ArgumentNullExceptionThe obj parameter is null.
T:System.Threading.SynchronizationLockExceptionWait is not invoked from within a synchronized block of code.
T:System.Threading.ThreadInterruptedExceptionThe thread that invokes Wait is later interrupted from the waiting state. This happens when another thread calls this thread's M:System.Threading.Thread.Interrupt method.
T:System.ArgumentOutOfRangeExceptionThe timeout parameter is negative and does not represent F:System.Threading.Timeout.Infinite (–1 millisecond), or is greater than F:System.Int32.MaxValue.

Definition at line 222 of file Monitor.cs.

◆ Wait() [3/5]

static bool System.Threading.Monitor.Wait ( object  obj,
int  millisecondsTimeout 
)
static

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.

Parameters
objThe object on which to wait.
millisecondsTimeoutThe 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.
Exceptions
T:System.ArgumentNullExceptionThe obj parameter is null.
T:System.Threading.SynchronizationLockExceptionThe calling thread does not own the lock for the specified object.
T:System.Threading.ThreadInterruptedExceptionThe thread that invokes Wait is later interrupted from the waiting state. This happens when another thread calls this thread's M:System.Threading.Thread.Interrupt method.
T:System.ArgumentOutOfRangeExceptionThe value of the millisecondsTimeout parameter is negative, and is not equal to F:System.Threading.Timeout.Infinite.

Definition at line 237 of file Monitor.cs.

◆ Wait() [4/5]

static bool System.Threading.Monitor.Wait ( object  obj,
TimeSpan  timeout 
)
static

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.

Parameters
objThe object on which to wait.
timeoutA T:System.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.
Exceptions
T:System.ArgumentNullExceptionThe obj parameter is null.
T:System.Threading.SynchronizationLockExceptionThe calling thread does not own the lock for the specified object.
T:System.Threading.ThreadInterruptedExceptionThe thread that invokes Wait is later interrupted from the waiting state. This happens when another thread calls this thread's M:System.Threading.Thread.Interrupt method.
T:System.ArgumentOutOfRangeExceptionThe value of the timeout parameter in milliseconds is negative and does not represent F:System.Threading.Timeout.Infinite (–1 millisecond), or is greater than F:System.Int32.MaxValue.

Definition at line 252 of file Monitor.cs.

◆ Wait() [5/5]

static bool System.Threading.Monitor.Wait ( object  obj)
static

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

Parameters
objThe 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.
Exceptions
T:System.ArgumentNullExceptionThe obj parameter is null.
T:System.Threading.SynchronizationLockExceptionThe calling thread does not own the lock for the specified object.
T:System.Threading.ThreadInterruptedExceptionThe thread that invokes Wait is later interrupted from the waiting state. This happens when another thread calls this thread's M:System.Threading.Thread.Interrupt method.

Definition at line 265 of file Monitor.cs.


The documentation for this class was generated from the following file: