Jon Jon - 1 year ago 789
C# Question

How to empty a BlockingCollection

I have a thread adding items to a


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
however I can't find a way to do it.

Any ideas?

Answer Source

Possibly use the overload of GetConsumingEnumerable which takes a CancellationToken, and then if anything goes wrong from the producing side, it can cancel the consumer.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download