// parameters.Count == 10
// actualFreeLicenses == 2
Parallel.For(0, parameters.Count, new ParallelOptions()
{
MaxDegreeOfParallelism = actualFreeLicenses
}, i =>
{
ExternalProgram(i);
}
);
When I execute the above code I notice that the value of i
passed to the ExternalProgram
method are 1 & 6, later 2 & 7, later 3 & 8 ...
If I have 14 Parameters and 2 licenses it always launch 1 & 8, later 2 & 9 ...
Is it possible to define order: first 1 & 2, later 3 & 4 etc?
How about using a Queue/ConcurrentQueue and dequeueing items in the body of your parallel loop? This will ensure that ordering is preserved.
If you are using Parallel the order in which they are executed is not of relevance therefore "Parallel". You should use a sequential workflow if the order is relevant for you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With