I have a thread which is adding items to a BlockingCollection. I want to clear the BlockingCollection. Can somebody help me with this?
Asked
Active
Viewed 900 times
2 Answers
0
You may need to read all the items from the BlockingCollection and assign to Discards
while (collection.TryTake(out _)){}

Mukul Keshari
- 495
- 2
- 7
0
Possibly use the overload of GetConsumingEnumerable
that takes a CancellationToken;
then, if anything goes wrong from the producing side, it can cancel the consumer.
And you can use it
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);
}
}
And you can even use this site

Amankhani MohammadJavad
- 695
- 5
- 7