.NET Framework Class Library
Monitor..::.Enter Method (Object, Boolean%)

[This documentation is for preview only, and is subject to change in later releases. Blank topics are included as placeholders.]

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)
Syntax

Visual Basic (Declaration)
Public Shared Sub Enter ( _
    obj As Object, _
    ByRef lockTaken As Boolean _
)
Visual Basic (Usage)
Dim obj As Object
Dim lockTaken As Boolean

Monitor.Enter(obj, lockTaken)
C#
public static void Enter(
    Object obj,
    ref bool lockTaken
)
Visual C++
public:
static void Enter(
    Object^ obj, 
    bool% lockTaken
)
F#
static member Enter : 
        obj:Object * 
        lockTaken:bool byref -> unit 

Parameters

obj
Type: System..::.Object
The object on which to wait.
lockTaken
Type: System..::.Boolean%
After the method completes, the variable specified for lockTaken contains a value that indicates whether the lock was successfully taken. The value is set whether or not the method throws an exception.
Exceptions

ExceptionCondition
ArgumentNullException

The obj parameter is nullNothingnullptra null reference (Nothing in Visual Basic).

Remarks

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.

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.

Use a C# tryfinally block (TryFinally in Visual Basic) to ensure that you release the monitor, or use the C# lock statement (SyncLock in Visual Basic), which wraps the Exit method in a tryfinally block.

Examples

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 needs to be released.

Visual Basic
Dim acquiredLock As Boolean = False

Try
    Monitor.Enter(lockObject, acquiredLock)

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

Finally
    If acquiredLock Then
        Monitor.Exit(lockObject)
    End If
End Try
C#
bool acquiredLock = false;

try
{
    Monitor.Enter(lockObject, ref acquiredLock);

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

}
finally
{
    if (acquiredLock)
    {
        Monitor.Exit(lockObject);
    }
}
Platforms

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Version Information

.NET Framework

Supported in: 4

.NET Framework Client Profile

Supported in: 4
See Also

Reference

Page view tracker