Using Blocking Collection as IEnumerable

The BlockingCollection class can be used with foreach loops to allow consumers to safely take items from the collection. However, the input to the loop must be the result of the BlockingCollection.GetConsumingEnumerable method and not the collection itself.

If the collection is used, the foreach loop will immediately exit if it is executed before the first item has been added to the collection by the producer. The BlockingCollection class implements the IEnumerable<> interface, so the compiler will not generate an error for this problem.

0 0

Post a comment