If I have the following order in an IEnumerable (list) :
1 – 2 – 3 – 4 – 5
and If I run a PLINQ query on this say :
list.AsParallel().AsOrdered().WithDegreeOfParallelism(10).Select(
s => SomeDelegateFunction(s)).AsSequential().ToList();
For the above query I see in my logging (inside delegate function) that it uses multiple threads, but does not maintain the sequence of processing of the list.
However for the below query i does maintain the sequence but uses a single thread to do the entire operation:
list.AsParallel().AsOrdered().WithDegreeOfParallelism(10).AsSequential().Select(
s => SomeDelegateFunction(s)).ToList();
The difference between the two queries is the “AsSequential()” in the second query, the question I had is that when I use AsSequential() :
1 – Why does it not use multi threads? It could have broken down the work as :
1 - 2 (Give it to thread 1)
3 - 4 - 5 (Give it to thread 2)
Instead it does do 1 – 2 – 3 – 4 – 5 (In that order) but does it on a single thread – why??
Basically I need to process my list in the ORDER it came into as input but on multiple threads.
Any ideas ?
Those are mutually exclusive requirements. You can do one or the other, but never both.
If you don’t care what order the items were processed in, you just want to make sure that your end result is that the objects are in the order that they first came in as you can do something like this:
If you prefer PLINQ over
Parallel.Foryou could do something like this: