Monitor.IsEntered Method

July 28, 2014

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

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

public static bool IsEntered(
	Object obj
)

Parameters

obj
Type: System.Object
The object to test.

Return Value

Type: System.Boolean
true if the current thread holds a lock on obj; otherwise, false.

The IsEntered method applies to locks that are taken by using the methods of the Monitor class (that is, the Enter, TryEnter, and Wait methods), and to locks that are taken by using the C# lock statement and the Visual Basic SyncLock statement, because the implementations of those statements use the Monitor class.

Use the IsEntered method with asserts and code contracts to diagnose problems with locks. For example, the following code shows a method that begins by asserting that the current thread has taken a lock on a global object.

static void ExampleMethod()
{
    Debug.Assert(Monitor.IsEntered(globalLockObject));
    // Other code.
}

Windows Phone OS

Supported in: 8.1, 8.0

Show:
© 2014 Microsoft