【发布时间】:2023-03-23 04:06:01
【问题描述】:
我正在运行一个输入列表的方法;此列表由用户提供。如果在处理一个输入时发生异常,我必须从输出表中删除它。代码如下所示:
List<Task> methodsList = new List<Task>();
for (int i = 0; i < inputList.Count; i++)
{
int arg = i;
Task newTask = Task.Factory.StartNew(() => ProcessInput(i));
methodsList.Add(newTask);
}
if (methodsList.Count != 0)
{
try
{
Task.WaitAll(methodsList.ToArray());
}
catch (AggregateException ex)
{
foreach (Exception innerEx in ex.InnerExceptions)
{
throw innerEx;
}
}
}
问题是,当我等待所有任务完成时,AggregatedException 并没有给我任何关于哪个线程失败的信息。我正在考虑通过创建一个集合并将已完成进程的索引添加到该列表并在最后检查它以查看哪些输入未处理来处理此问题,但我想知道是否有更简单的方法。
【问题讨论】:
标签: c# multithreading visual-studio-2010 exception-handling task