Why Parallel.ForEach
loop exits with OperationCancelledException
, while using GetConsumableEnumerable
?
//outside the function
static BlockingCollection<double> _collection = new BlockingCollection<double>();
var t = Task.Factory.StartNew(Producer);
Parallel.ForEach(_collection.GetConsumingEnumerable(),
item => Console.WriteLine("Processed {0}", item));
Console.WriteLine("FINISHED processing");
public static void Producer()
{
var data = Enumerable.Range(1, 1000);
foreach (var i in data)
{
_collection.Add(i);
Console.WriteLine("Added {0}",i);
}
Console.WriteLine("Finished adding");
_collection.CompleteAdding();
}
OperationCancelledException
behavior on .NET 7. The code in the question runs successfully to completion, no exception is thrown. – Locarno