Click or drag to resize

MonitorLock Class

MonitorLock is a class for assisting people with synchronized operations. Traditionally, code might have looked something like this:
lock( object ) { 
  ...
}
However, this has a few issues. It's prone to deadlock because the lock operator does not have a timeout. It's also difficult to determine who owns a lock at a given time. So eventually people changed to this form:
if (Monitor.TryEnter(object, timeout)) {
  try {
   ...
  } finally {
    Monitor.Exit(object);
  }
}
It gets bulky and begins to become difficult to maintain over time. MonitorLock works much like the lock( object ) model except that it relies upon the IDisposable interface to help with scoping of the lock. So to use MonitorLock, first instantiate one and then replace your lock(object) with this:
using(lockObj.Acquire()) {
  ...
}
Your code will work as before except that the monitorLock will use a timed entry into critical sections and it can be used to diagnose issues that may be occuring in your thread locking.

MonitorLock allows users to specify events that can be consumed on lock acquisition or release. Additionally, it can inform you when a lock is acquired within an existing lock. And last, if you want to know where your locks are being acquired, it can maintain a StackTrace of points where allocations are occuring.

Inheritance Hierarchy
SystemObject
  com.espertech.esper.compat.threading.locksMonitorLock

Namespace:  com.espertech.esper.compat.threading.locks
Assembly:  NEsper.Compat (in NEsper.Compat.dll) Version: 8.0.0.0
Syntax
C#
public class MonitorLock : ILockable

The MonitorLock type exposes the following members.

Constructors
  NameDescription
Public methodMonitorLock
Initializes a new instance of the MonitorLock class.
Public methodMonitorLock(Int32)
Initializes a new instance of the MonitorLock class.
Top
Properties
  NameDescription
Public propertyIsHeldByCurrentThread
Gets a value indicating whether this instance is held by current thread.
Public propertyLockDepth
Gets the lock depth.
Public propertyLockTimeout
Gets the number of milliseconds until the lock acquisition fails.
Top
Methods
Extension Methods
See Also