public int Release() { TaskNode head = null; int count; do { lock (_lockObj) { count = _currentCount; if (_head == null) { ++_currentCount; head = null; } else { head = _head; if (_head == _tail) { _head = _tail = null; } else { _head = _head.Next; } } } } while (head != null && head.Task.IsCompleted); if (head != null) { if (head.Cancellation != default(CancellationTokenRegistration)) { head.Cancellation.Dispose(); } Task.Run(() => head.TrySetResult(false)); } return(count); }