Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Task.WaitAll, how to find the tasks causing AggregateException

Let's say that I got the following code:

var tasks = BuildTaskList();
try
{
    Task.WaitAll(tasks.ToArray());
}
catch (AggregateException exception)
{

}

How do I know which task threw which of the exceptions in exception.InnerExceptions?

like image 674
jgauffin Avatar asked Aug 19 '13 12:08

jgauffin


1 Answers

You still have the list of Tasks, and each Task has an Exception property. Using that you can figure out which exceptions belong with which Task.

But, if you can, it'd be better to use Task.WhenAll or TaskFactory.ContinueWhenAll than do a blocking Wait.

like image 76
Matt Smith Avatar answered Oct 09 '22 15:10

Matt Smith