I have a list of works to do. and i want to run them in parallel in an ordered way. but ordered parallel is trying to split methods into chunks and then execute chunks in ordered way.
Here is the reproduced problem.
var list = Enumerable.Range(0, 1000).ToList();
list.AsParallel().AsOrdered().ForAll(i =>
{
Task.Delay(1000).Wait();
Console.WriteLine(i);
});
I expect this to first print values from start. like or something like this
1
0
2
3
But here is the results.
This means ordered parallel is dividing list into chunks and starts executing them in order. but i dont want this to execute tasks from middle of list. How can i fix this behavior?
I noticed if i dont use List and instead use IEnumerable it fixes the problem and prevents splitting. I think its because the Enumerable does not hold the all values yet so dividing is not possible.
var list = Enumerable.Range(0, 1000); // removed ToList
The results will change to this which is what i want.
But i have populated a list. i dont have pure ienumerable how ever i tried
list.AsEnumerable().AsParallel().AsOrdered()....
But parallel still starts to split the list. Is there any parallel option to avoid this?
If not the question may change to how to explicitly convert List into Enumerable? here list.AsEnumerable()
still seems to be potentially list. how to get pure enumerable ?