Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between "foreach with task.Wait" and Task.WaitAll

What is the difference between:

foreach(Task task in someTasks)
{
    task.Wait();
}

and

Task.WaitAll(sometasks);

In essence, looking from the end result perspective, they should be doing the same thing?

like image 955
Denis Biondic Avatar asked Sep 12 '25 23:09

Denis Biondic


1 Answers

The difference is in the exception handling. If task.Wait(); throws, the foreach loop is exited. WaitAll will catch all exceptions and throw an AggregateException containing them.

See also: Task.WaitAll and Exceptions

like image 171
Henrik Avatar answered Sep 15 '25 12:09

Henrik