How to empty a BlockingCollection
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 _)){}