Task.WaitAll,如何找到导致 AggregateException 的任务 [英] Task.WaitAll, how to find the tasks causing AggregateException

查看:29
本文介绍了Task.WaitAll,如何找到导致 AggregateException 的任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我得到了以下代码:

Let's say that I got the following code:

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

}

我如何知道哪个任务在 exception.InnerExceptions 中抛出了哪些异常?

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

推荐答案

你还有Tasks的列表,每个Task都有一个Exception代码>属性.使用它,您可以找出哪些异常属于哪个 Task.

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.

但是,如果可以,最好使用 Task.WhenAllTaskFactory.ContinueWhenAll 而不是阻塞等待.

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

这篇关于Task.WaitAll,如何找到导致 AggregateException 的任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆