C
C#2y ago
Tandy

❔ How to pause BlockingCollection consumption?

I'm currently enumerating through a BlockingCollection (with an underlying ConcurrentQueue backing class) using GetConsumingEnumerable(). But I have to pause execution based on some logic, leaving items on the queue, until some other business logic resumes execution. Anyone know the best practice for that? Do I have to bypass GetConsumingEnumerable() and instead write something like below, but adding pause logic?
while (!IsCompleted)
{
if (TryTake(out var item, Timeout.Infinite))
yield return item;
}
while (!IsCompleted)
{
if (TryTake(out var item, Timeout.Infinite))
yield return item;
}
2 Replies
Tandy
Tandy2y ago
Thank you @calledude I had seen a few references to ManualResetEvent in my searches but didn't quite understand it. I did end up implementing it, haven't tested yet, but very happy to come back and see you recommended the same!
Accord
Accord2y ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.