Mahdi Ghiasi
Mahdi Ghiasi

Reputation: 15301

Parallel For loops. Are they wait for finish?

I have two for loops. which the second loop must be started after finishing the first loop.

So, If I use two Parallel.For() loops, will the second loop runs after the finishing the first loop?

Upvotes: 27

Views: 18367

Answers (1)

Ray
Ray

Reputation: 46585

Yes. Parallel.For will not return until all operations have completed.

If you run

Parallel.For(0, 5, i => Console.WriteLine("First {0}", i));
Console.WriteLine("First Finished");
Parallel.For(0, 5, i => Console.WriteLine("Second {0}", i));
Console.WriteLine("Second Finished");

The output is

First 0
First 2
First 1
First 4
First 3
First Finished
Second 0
Second 4
Second 3
Second 2
Second 1
Second Finished

The order of the integers will vary, but second will always come after first.

Upvotes: 41

Related Questions