I have experimented with both of these loops and came to notice that even though regular foreach loop in Task's Action delegate supposed to perform in parallel, it doesn't process elements in parallel. However if i replace it with Parallel.ForEach i see data is being processed in parallel across multiple threads.
Code 1:
Task loadingTask1 = Factory.StartNew(() => { foreach (MyOneClass dg in Queue.GetConsumingEnumerable()) { MyOtherClass vl = new MyOtherClass(); vl.Id = dg.Id; vl.PerformTimeConsumingAction(); OutputQueue.Add(vl); } }); Code 2:
Task loadingTask2 = Factory.StartNew(() => { Parallel.ForEach(Queue.GetConsumingEnumerable(), (dg) => { MyOtherClass vl = new MyOtherClass(); vl.Id = dg.Id; vl.PerformTimeConsumingAction(); OutputQueue.Add(vl); }); }); Code 1 when run with Console.Write statement on each iteration seems to be waiting for the previous cycle to complete till it grabs next one, but Code 2 does process multiple elements in parallel.
Am i not understanding regular foreach in Task.Action correctly? I thought .NET would start as many threads for the task as load warrants and each iteration of foreach would be processed in parallel.
I have also tried passing PLINQ result to both of the above codes and observer same behavior: regular foreach seemed to wait for the previous iteration to complete to start the next one, even though i have used .AsParallel() and .WithExecutionMode(ParallelExecutionMode.ForceParallelism) directives.
Any insight would be highly appreciated. I am aware of OrderingPartitioner class and may try using it

Parallel.ForEach().