Does Parallel.Foreach support collection order?

Is there a way to guarantee order when using Parallel.ForEach()? The collection I'm looping should support its order, but I was looking for some performance improvements.

+3
source share
3 answers

no, ForEach is used only for conditions under which the order does not matter; try Parallel.For

-1
source

So, do you have a statement that looks something like this? (based on your comments above).

Parallel.Foreach(myData, ..., (d) =>
{
  StringBuilder sb = new StringBuilder();
  sb.Append(d);
  // WriteLine sb?
});

There are a number of problems with this approach.

  • Parallel.For, Parallel.ForEach , myData .
  • Console StringBuilder , , , , .

, . , , AsOrdered() PLINQ, , .
. MSDN PLINQ ForAll

, , . , , .

+6

For anyone looking for a simple solution, I published 2 extension methods (one using PLINQ and one using Parallel.ForEach) as part of the answer to the following question:

Order PLINQ ForAll

0
source

Source: https://habr.com/ru/post/1773886/


All Articles