Monitor.Enter Method (Object, Boolean)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

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

Namespace:   System.Threading
Assembly:  mscorlib (in mscorlib.dll)

public static void Enter(
	object obj,
	ref bool lockTaken


Type: System.Object

The object on which to wait.

Type: System.Boolean

The 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.

Exception Condition

The input to lockTaken is true.


The obj parameter is null.

Use Enter to acquire the Monitor on the object passed as the obj parameter. If another thread has executed an Enter on the object but has not yet executed the corresponding Exit, 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.

If the lock was not taken because an exception was thrown, the variable specified for the lockTaken parameter is false after this method ends. This allows the program to determine, in all cases, whether it is necessary to release the lock. If this method returns without throwing an exception, the variable specified for the lockTaken parameter is always true, and there is no need to test it.

Use Monitor 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, another separate object is created. Because the object passed to Exit is different from the object passed to Enter, Monitor throws SynchronizationLockException. For more information, see the conceptual topic Monitors.

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

The following code shows the basic pattern for using the Enter(Object, Boolean) method overload. This overload always sets the value of the variable that is passed to the ref parameter (ByRef in Visual Basic) lockTaken, even if the method throws an exception, so the value of the variable is a reliable way to test whether the lock has to be released.

bool acquiredLock = false;

    Monitor.Enter(lockObject, ref acquiredLock);

    // Code that accesses resources that are protected by the lock.

    if (acquiredLock)

Universal Windows Platform
Available since 8
.NET Framework
Available since 4.0
Portable Class Library
Supported in: portable .NET platforms
Available since 4.0
Windows Phone Silverlight
Available since 7.1
Windows Phone
Available since 8.1
Return to top