Why use while(true) with .take with a blockingcollection?

413 views Asked by At

I have been looking into using blockingcollection. I found this article very helpful

Although, he is using "tasks" for multi threading purposes, I don't think I need this. But anyways, the author demonstrate 2 ways to iterate through the collection to remove items. The first way:

while (true)
{
    Console.WriteLine("Worker 2: " + blockingCollection.Take());
}

And the second way:

foreach (string value in blockingCollection.GetConsumingEnumerable())
{
    Console.WriteLine("Worker 1: " + value);
}

The author explains that the second way is more elegant so I plan to use the second way. However, just for my understanding. Can someone explain to me the while(true) loop in the first technique? I don't understand, I have been reading the Microsoft docs and google... But haven't found anything that would indicate that ".Take()" returns true or false under any circumstances...

0

There are 0 answers