I have a thread adding items to a BlockingCollection
.
On another thread I am using foreach (var item in myCollection.GetConsumingEnumerable())
If there is a problem I want to break out of my foreach and my method and clear whatever is left in the BlockingCollection
however I can't find a way to do it.
Any ideas?
I'm using this extension method:
public static void Clear<T>(this BlockingCollection<T> blockingCollection) { if (blockingCollection == null) { throw new ArgumentNullException("blockingCollection"); } while (blockingCollection.Count > 0) { T item; blockingCollection.TryTake(out item); } }
I'm wondering if there's a better, less hacky, solution.
Just take out all remaining items:
while (collection.TryTake(out _)){}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With