What is the difference between:
Parallel.ForEach(sometasks, x => x.Wait());
and
Task.WaitAll(sometasks);
EDIT:
Well I am not fully sure why I included Parallel into the question :/ ... What I had in mind was:
foreach(Task task in someTasks)
{
task.Wait();
}
but I just included Parallel.ForEach thinking it is the same thing, while of course its not --- it spawns a thread for each task to wait, which is big overhead.
Though Parallel.ForEach(sometasks, x => x.Wait()); makes no sense for me.
Short answer is
Parallel.ForEach: With your code, blocks at least two threads. Calling thread and ThreadPool thread(s) as well.
Task.WaitAll: Blocks only the calling thread.
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