private void Process() { while (!(_processCancellationTokenSource?.IsCancellationRequested ?? true)) { if (_queue?.Count > 0) { do { IQueueItem item = _queue?.Take(); item?.Do(); if (_processCancellationTokenSource?.IsCancellationRequested ?? true) { break; } } while (_queue?.Count > 0); _queueAddedEvent?.Reset(); continue; } if (_processCancellationTokenSource != null && _queueAddedEvent != null) { Debug.WriteLine("Waiting for add next item..."); int ret = WaitHandle.WaitAny(new[] { _processCancellationTokenSource.Token.WaitHandle, _queueAddedEvent }); if (ret == 0) { break; } if (_queue?.Count == 0) { _queueAddedEvent?.Reset(); } } else { break; } Debug.WriteLine("Queue item arrived"); } Debug.WriteLine("Process task is finished"); }