BCL mscorlib [00 00 00 00 00 00 00 00 04 00 00 00 00 00 00 00 ] 1.0.5000.0 2.0.0.0 4.0.0.0 All public static members of this type are safe for multithreaded operations. No instance members are guaranteed to be thread safe. System.Object System.Runtime.InteropServices.ComVisible(true)

The Monitor class controls access to objects by granting a lock for an object to a single thread. Object locks provide the ability to restrict access to a block of code, commonly called a critical section. While a thread owns the lock for an object, no other thread can acquire that lock. You can also use Monitor to ensure that no other thread is allowed to access a section of application code being executed by the lock owner, unless the other thread is executing the code using a different locked object.

Use to lock objects (that is, reference types), not value types. For details, see the overloads of the method and the conceptual topic [<topic://cpconMonitor>].

Monitor has the following features:

It is associated with an object on demand.

It is unbound, which means it can be called directly from any context.

An instance of the Monitor class cannot be created.

The following information is maintained for each synchronized object:

A reference to the thread that currently holds the lock.

A reference to a ready queue, which contains the threads that are ready to obtain the lock.

A reference to a waiting queue, which contains the threads that are waiting for notification of a change in the state of the locked object.

The following table describes the actions that can be taken by threads that access synchronized objects:

Action

Description

,

Acquires a lock for an object. This action also marks the beginning of a critical section. No other thread can enter the critical section unless it is executing the instructions in the critical section using a different locked object.

Releases the lock on an object in order to permit other threads to lock and access the object. The calling thread waits while another thread accesses the object. Pulse signals are used to notify waiting threads about changes to an object's state.

(signal),

Sends a signal to one or more waiting threads. The signal notifies a waiting thread that the state of the locked object has changed, and the owner of the lock is ready to release the lock. The waiting thread is placed in the object's ready queue so that it might eventually receive the lock for the object. Once the thread has the lock, it can check the new state of the object to see if the required state has been reached.

Releases the lock on an object. This action also marks the end of a critical section protected by the locked object.

Use the Enter and Exit methods to mark the beginning and end of a critical section. If the critical section is a set of contiguous instructions, then the lock acquired by the Enter method guarantees that only a single thread can execute the enclosed code with the locked object. In this case, it is recommended you place those instructions in a try block and place the Exit instruction in a finally block. This facility is typically used to synchronize access to a static or instance method of a class. The functionality provided by the Enter and Exit methods is identical to that provided by the C# lock statement (SyncLock in Visual Basic), except that lock and SyncLock wrap the method overload and the method in a try…finally block (Try…Finally in Visual Basic) to ensure that the monitor is released.

Beginning with the net_v40_long, there are two sets of overloads for the and methods. One set of overloads has a ref (ByRef in Visual Basic) parameter that is atomically set to true if the lock is acquired, even if an exception is thrown when acquiring the lock. Use these overloads if it is critical to release the lock in all cases, even when the resources the lock is protecting might not be in a consistent state.

If a critical section spans an entire method, the locking facility described above can be achieved by placing the on the method, and specifying the value in the constructor of MethodImplAttribute. Using this attribute, the Enter and Exit statements are not needed. Note that the attribute causes the current thread to hold the lock until the method returns; if the lock can be released sooner, use the Monitor class or the C# lock statement instead of the attribute.

While it is possible for the Enter and Exit statements that lock and release a given object to cross member or class boundaries or both, this practice is not recommended.

When selecting an object on which to synchronize, you should lock only on private or internal objects. Locking on external objects might result in deadlocks, because unrelated code could choose the same objects to lock on for different purposes.

Provides a mechanism that synchronizes access to objects.

- Method
  1.0.5000.0
  2.0.0.0
  4.0.0.0
  System.Void `` is .

  Use Enter to acquire the on the object passed as the parameter. If another thread has executed an Enter on the object but has not yet executed the corresponding , the current thread will block until the other thread releases the object. It is legal for the same thread to invoke Enter more than once without it blocking; however, an equal number of Exit calls must be invoked before other threads waiting on the object will unblock.

  Use to lock objects (that is, reference types), not value types. When you pass a value type variable to Enter, it is boxed as an object. If you pass the same variable to Enter again, it is boxed as a separate object, and the thread does not block. In this case, the code that Monitor is supposedly protecting is not protected. Furthermore, when you pass the variable to Exit, still another separate object is created. Because the object passed to Exit is different from the object passed to Enter, Monitor throws . For more information, see the conceptual topic [<topic://cpconMonitor>].

  can interrupt threads that are waiting to enter a Monitor on an object. A will be thrown.

  Use a C# try…finally block (Try…Finally in Visual Basic) to ensure that you release the monitor, or use the C# lock statement (SyncLock statement in Visual Basic), which wraps the and methods in a try…finally block.

  Acquires an exclusive lock on the specified object.

  The object on which to acquire the monitor lock. 0
- Method
  4.0.0.0
  System.Void `` `` To be added. To be added. To be added. To be added.
- Method
  1.0.5000.0
  2.0.0.0
  4.0.0.0
  System.Runtime.ConstrainedExecution.ReliabilityContract(System.Runtime.ConstrainedExecution.Consistency.WillNotCorruptState, System.Runtime.ConstrainedExecution.Cer.Success)
  System.Void `` is .

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

  The calling thread must own the lock on the parameter. If the calling thread owns the lock on the specified object, and has made an equal number of Exit and calls for the object, then the lock is released. If the calling thread has not invoked Exit as many times as Enter, the lock is not released.

  If the lock is released and other threads are in the ready queue for the object, one of the threads acquires the lock. If other threads are in the waiting queue waiting to acquire the lock, they are not automatically moved to the ready queue when the owner of the lock calls Exit. To move one or more waiting threads into the ready queue, call or before invoking Exit.

  Releases an exclusive lock on the specified object.

  The object on which to release the lock. 0
- Method
  4.0.0.0
  System.Boolean ``

  This method works only for locks that are acquired by using the methods of the class, or by using the C# lock statement or the Visual Basic SyncLock statement, which are implemented with .

  Use this method with diagnostic tools, such as the method and the class, to debug locking issues that involve the class.

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

  true if the current thread holds the lock on ; otherwise, false.

  The object to test.
- Method
  1.0.5000.0
  2.0.0.0
  4.0.0.0
  System.Void `` is . The calling thread does not own the lock for the specified object.

  Only the current owner of the lock can signal a waiting object using Pulse.

  The thread that currently owns the lock on the specified object invokes this method to signal the next thread in line for the lock. Upon receiving the pulse, the waiting thread is moved to the ready queue. When the thread that invoked Pulse releases the lock, the next thread in the ready queue (which is not necessarily the thread that was pulsed) acquires the lock.

  The class does not maintain state indicating that the method has been called. Thus, if you call when no threads are waiting, the next thread that calls blocks as if had never been called. If two threads are using and to interact, this could result in a deadlock. Contrast this with the behavior of the class: If you signal an by calling its method, and there are no threads waiting, the remains in a signaled state until a thread calls , , or . The releases that thread and returns to the unsignaled state.

  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, , and methods must be invoked from within a synchronized block of code.

  To signal multiple threads, use the method.

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

  The object a thread is waiting for. 0
- Method
  1.0.5000.0
  2.0.0.0
  4.0.0.0
  System.Void `` is . The calling thread does not own the lock for the specified object.

  The thread that currently owns the lock on the specified object invokes this method to signal all threads waiting to acquire the lock on the object. After the signal is sent, the waiting threads are moved to the ready queue. When the thread that invoked PulseAll releases the lock, the next thread in the ready queue acquires the lock.

  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 , PulseAll, and methods must be invoked from within a synchronized block of code.

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

  To signal a single thread, use the Pulse method.

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

  The object that sends the pulse. 0
- Method
  1.0.5000.0
  2.0.0.0
  4.0.0.0
  System.Boolean `` is .

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

  This method is similar to , 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.

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

  Attempts to acquire an exclusive lock on the specified object.

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

  The object on which to acquire the lock. 0
- Method
  4.0.0.0
  System.Void `` `` To be added. To be added. To be added. To be added.
- Method
  1.0.5000.0
  2.0.0.0
  4.0.0.0
  System.Boolean `` `` is . is negative, and not equal to . The number of milliseconds to wait for the lock.

  If the parameter equals , this method is equivalent to . If equals 0, this method is equivalent to .

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

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

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

  The object on which to acquire the lock. The number of milliseconds to wait for the lock. 0
- Method
  1.0.5000.0
  2.0.0.0
  4.0.0.0
  System.Boolean `` `` is . The value of in milliseconds is negative and is not equal to , or is greater than . A representing the amount of time to wait for the lock. A value of –1 millisecond specifies an infinite wait.

  If the value of the parameter converted to milliseconds equals –1, this method is equivalent to . If the value of equals 0, this method is equivalent to .

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

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

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

  The object on which to acquire the lock. A representing the amount of time to wait for the lock. A value of –1 millisecond specifies an infinite wait. 0
- Method
  4.0.0.0
  System.Void `` `` `` To be added. To be added. To be added. To be added. To be added.
- Method
  4.0.0.0
  System.Void `` `` `` To be added. To be added. To be added. To be added. To be added.
- Method
  1.0.5000.0
  2.0.0.0
  4.0.0.0
  System.Boolean `` is . The calling thread does not own the lock for the specified object.

  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 or , 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 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 , , and Wait methods must be invoked from within a synchronized block of code.

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

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

  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.

  The object on which to wait. 0
- Method
  1.0.5000.0
  2.0.0.0
  4.0.0.0
  System.Boolean `` `` is . The calling thread does not own the lock for the specified object. The value of is negative, and not equal to .

  This method does not return until it reacquires an exclusive lock on the 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 or 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 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 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 , or it is the next in the queue and a thread that holds the lock invokes . However, if elapses before another thread invokes this object's or method, the original thread is moved to the ready queue in order to regain the lock.

  If is specified for the parameter, this method blocks indefinitely unless the holder of the lock calls or . If 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 has been invoked for the specified object. Conceptually, the Wait method stores the number of times the caller invoked on the object and invokes 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 as many times as necessary to restore the saved 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.

  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 , , and Wait methods must be invoked from within a synchronized block of code.

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

  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.

  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.

  The object on which to wait. The number of milliseconds to wait before the thread enters the ready queue. 0
- Method
  1.0.5000.0
  2.0.0.0
  4.0.0.0
  System.Boolean `` `` is . The calling thread does not own the lock for the specified object. If is negative, and is not equal to , or is greater than .

  This method does not return until it reacquires an exclusive lock on the 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 or 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 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 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 , or it is the next in the queue and a thread that holds the lock invokes . However, if elapses before another thread invokes this object's or method, the original thread is moved to the ready queue in order to regain the lock.

  If a representing –1 millisecond is specified for the parameter, this method blocks indefinitely unless the holder of the lock calls or . If 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 has been invoked for the specified object. Conceptually, the Wait method stores the number of times the caller invoked on the object and invokes 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 as many times as necessary to restore the saved 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.

  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 , , and Wait methods must be invoked from within a synchronized block of code.

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

  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.

  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.

  The object on which to wait. A representing the amount of time to wait before the thread enters the ready queue. 0
- Method
  1.0.5000.0
  2.0.0.0
  4.0.0.0
  System.Boolean `` `` ``

  This method does not return until it reacquires an exclusive lock on the 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 or 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 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 method again.

  When a thread calls Wait, it releases the lock and enters the waiting queue. At this point, the next thread in the ready queue (if there is one) is allowed to take control of the lock. The thread that invoked Wait remains in the waiting queue until either a thread that holds the lock invokes , or it is the next in the queue and a thread that holds the lock invokes . However, if elapses before another thread invokes this object's or method, the original thread is moved to the ready queue in order to regain the lock.

  If is specified for the parameter, this method blocks indefinitely unless the holder of the lock calls or . If 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 has been invoked for the specified object. Conceptually, the Wait method stores the number of times the caller invoked on the object and invokes 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 as many times as necessary to restore the saved 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.

  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 , , and Wait methods must be invoked from within a synchronized block of code.

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

  ## Notes on Exiting the Context

  The parameter has no effect unless the method is called from inside a nondefault managed context. This can happen if your thread is inside a call to an instance of a class derived from . Even if you are currently executing a method on a class that is not derived from , like , you can be in a nondefault context if a is on your stack in the current application domain.

  When your code is executing in a nondefault context, specifying true for causes the thread to exit the nondefault managed context (that is, to transition to the default context) before executing the method. It returns to the original nondefault context after the call to the method completes.

  This can be useful when the context-bound class has the attribute applied. In that case, all calls to members of the class are automatically synchronized, and the synchronization domain is the entire body of code for the class. If code in the call stack of a member calls the method and specifies true for , the thread exits the synchronization domain, allowing a thread that is blocked on a call to any member of the object to proceed. When the method returns, the thread that made the call must wait to reenter the synchronization domain.

  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.

  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.

  The object on which to wait. The number of milliseconds to wait before the thread enters the ready queue. true to exit and reacquire the synchronization domain for the context (if in a synchronized context) before the wait; otherwise, false.
- Method
  1.0.5000.0
  2.0.0.0
  4.0.0.0
  System.Boolean `` `` ``

  This method does not return until it reacquires an exclusive lock on the 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 or 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 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 method again.

  When a thread calls Wait, it releases the lock and enters the waiting queue. At this point, the next thread in the ready queue (if there is one) is allowed to take control of the lock. The thread that invoked Wait remains in the waiting queue until either a thread that holds the lock invokes , or it is the next in the queue and a thread that holds the lock invokes . However, if milliseconds elapse before another thread invokes this object's or method, the original thread is moved to the ready queue in order to regain the lock.

  If a representing -1 millisecond is specified for the parameter, this method blocks indefinitely unless the holder of the lock calls or . If 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 has been invoked for the specified object. Conceptually, the Wait method stores the number of times the caller invoked on the object and invokes 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 as many times as necessary to restore the saved 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.

  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 , , and Wait methods must be invoked from within a synchronized block of code.

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

  ## Notes on Exiting the Context

  The parameter has no effect unless the method is called from inside a nondefault managed context. This can happen if your thread is inside a call to an instance of a class derived from . Even if you are currently executing a method on a class that is not derived from , like , you can be in a nondefault context if a is on your stack in the current application domain.

  When your code is executing in a nondefault context, specifying true for causes the thread to exit the nondefault managed context (that is, to transition to the default context) before executing the method. It returns to the original nondefault context after the call to the method completes.

  This can be useful when the context-bound class has the attribute applied. In that case, all calls to members of the class are automatically synchronized, and the synchronization domain is the entire body of code for the class. If code in the call stack of a member calls the method and specifies true for , the thread exits the synchronization domain, allowing a thread that is blocked on a call to any member of the object to proceed. When the method returns, the thread that made the call must wait to reenter the synchronization domain.

  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.

  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.

  The object on which to wait. A representing the amount of time to wait before the thread enters the ready queue. true to exit and reacquire the synchronization domain for the context (if in a synchronized context) before the wait; otherwise, false.
0