private void Lock_Disposed(object sender, EventArgs e) { DistributedLock disposingLock = (DistributedLock)sender; disposingLock.Disposed -= Lock_Disposed; // Unsubscribe. //we need to remove this object from the lock collection lock (_currentLockLock) { // Only remove the lock if the one we're disposing is the original top-level lock for that key. if (_currentLockTurn == null || ReferenceEquals(_currentLockTurn, disposingLock) == false) { return; // Wasn't our current holder, so we don't care about it. } _currentLockTurn = null; // It's disposed, no longer current owner. if (_disposed == false) { // We're releasing the lock for this thread. We need to check if any other process has a request pending. // And if so, we need to force this process to wait a minimum delay, even if we don't have one waiting now. if (_lock != null && _provider.CheckLockRequest(_name)) { _minTimeNextTurn = DateTimeOffset.Now.AddMilliseconds(BackOffDelay); // Back off for a bit. _lock.Dispose(); // We have to give up the OS lock because other processes need a chance. _lock = null; } StartNextTurn(null); // Find and signal the next turn to go ahead (also handles all-done). } else { // We're already disposed, so we'd better release the lock and request now if we still have them! if (_lockRequest != null) { _lockRequest.Dispose(); _lockRequest = null; } if (_lock != null) { _lock.Dispose(); _lock = null; } } } }