While there are more complex and functional lock mechanisms, I wanted a very simple lock that would tell me if an operation was locked or not. Normally I would just have a flag that’d be true or false, but I found that it was not always easy to remember to set the flag on or off, and also to make sure the flag was set at the right place. So I came up with this class :
public class BooleanLock
{
public bool Locked { get; private set; }
public class BlockLock : IDisposable
{
private BooleanLock _parent;
public BlockLock(BooleanLock parent)
{
_parent = parent;
_parent.Locked = true;
}
#region IDisposable Members
public void Dispose()
{
_parent.Locked = false;
}
#endregion
}
public BlockLock Lock()
{
return new BlockLock(this);
}
}
Now I could use it this way :
// Declare a field as a lock
private BooleanLock _myUpdateLock = new BooleanLock();
// Check for lock
private void UpdateSomeProperty(bool reset)
{
if (_myUpdateLock.Locked)
return;
// . . .
And here’s where the lock is set :
// Part of the function is not re-entrant, so we use a lock there
private void OnSomePropertyChanged()
{
if (some-condition)
{
using (_myUpdateLock.Lock())
{
// do non-re-entrant stuff
} <-- lock is released here automatically
The big advantage here is that the C# compiler’s using-block behavior handles complex try-catch scenarios that I would otherwise have had to implement on my own.