18 using System.Collections.Concurrent;
19 using System.Collections.Generic;
20 using System.Threading;
31 private readonly ConcurrentQueue<T> _collection =
new ConcurrentQueue<T>();
32 private readonly ManualResetEventSlim _processingCompletedEvent =
new ManualResetEventSlim(
true);
33 private bool _completedAdding;
44 public int Count => _collection.Count;
49 public bool IsBusy => !_collection.IsEmpty || !_processingCompletedEvent.IsSet;
55 public void Add(T item)
57 Add(item, CancellationToken.None);
65 public void Add(T item, CancellationToken cancellationToken)
69 throw new InvalidOperationException(
"Collection has already been marked as not " +
70 $
"accepting more additions, see {nameof(CompleteAdding)}");
74 lock (_processingCompletedEvent)
77 _processingCompletedEvent.Reset();
78 _collection.Enqueue(item);
99 while (_collection.TryDequeue(out item))
105 lock (_processingCompletedEvent)
107 if (!_collection.TryPeek(out item))
109 _processingCompletedEvent.Set();
121 _processingCompletedEvent.Dispose();
129 _completedAdding =
true;