I want to increment an index on a particular value, for example 2:
for (int i = 0; i < 10; i+=2)
{
Console.WriteLine(i);
}
How do I do the same using the Parallel
class, like:
Parallel.For(0, 10, i =>
{
Console.WriteLine(i);
i += 2; //this a naïve assumption, it's not working
});
Edit
I would like the Parallel
loop to run only 5 operations (as the sequential for) and order doesn't matter for me.
Another approach would be to use where clause:
Parallel.ForEach(Enumerable.Range(0, 10).Where(i => i % 2 == 0), i =>
{
Console.WriteLine(i);
});
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