Reputation: 4594
Using the second example from Microsoft Docs, when I have a non-blocking consumer, what is the preferred approach to make consumer wait when there are no items in a BlockingCollection
? The example from the docs is as follows.
static void NonBlockingConsumer(BlockingCollection<int> bc, CancellationToken ct)
{
// IsCompleted == (IsAddingCompleted && Count == 0)
while (!bc.IsCompleted)
{
int nextItem = 0;
try
{
if (!bc.TryTake(out nextItem, 0, ct))
{
Console.WriteLine(" Take Blocked");
}
else
Console.WriteLine(" Take:{0}", nextItem);
}
catch (OperationCanceledException)
{
Console.WriteLine("Taking canceled.");
break;
}
// Slow down consumer just a little to cause
// collection to fill up faster, and lead to "AddBlocked"
Thread.SpinWait(500000);
}
Console.WriteLine("\r\nNo more items to take.");
}
The above example uses SpinWait
to pause the consumer.
Simply using the following would probably keep the CPU very busy.
if (!bc.TryTake(out var item))
{
continue;
}
What is the preferred approach here to make consumer wait? I am planning of using several BlockingCollection
s and looking for the most optimal way of using it.
Upvotes: 1
Views: 2074
Reputation: 23898
I would suggest using Take
rather than TryTake
.
A call to Take may block until an item is available to be removed.
The link you mentioned in your question has a good (blocking) example:
while (!dataItems.IsCompleted)
{
Data data = null;
// Blocks if number.Count == 0
// IOE means that Take() was called on a completed collection.
// Some other thread can call CompleteAdding after we pass the
// IsCompleted check but before we call Take.
// In this example, we can simply catch the exception since the
// loop will break on the next iteration.
try
{
data = dataItems.Take();
}
catch (InvalidOperationException) { }
if (data != null)
{
Process(data);
}
}
Console.WriteLine("\r\nNo more items to take.");
Upvotes: 3