我在.net 4.0中创建任务时遇到奇怪的异常。
我在使用Global Updomain未处理的异常处理程序的Windows服务上获得异常,因此我没有完全堆栈:A Task's exception(s) were not observed either by Waiting on the Task or accessing its Exception property.
我认为它发生在以下代码部分:
for (int i = 0; i < _workers.Length; ++i)
{
int j = i; // copy
Task t1 = Task.Factory.StartNew(() =>
{
try
{
if (!_workers[j].Join(4000))
LogWriter.Trace("Failed to join thread", "ThreadFailureOnDispose");
}
catch (Exception ex)
{
OnLogged(ex.Message + ex.StackTrace);
}
});
}
有人有个主意吗?它是否具有聚合异常功能?
答案 0 :(得分:1)
答案 1 :(得分:0)
您应该会发现异常属于AggregateException
类型(以便将来参考,在提问时包含异常类型 - 它是密钥信息)。
这包括其InnerExceptions
属性中抛出的异常。
答案 2 :(得分:0)
好吧,我认为在使用Parallel.For \ Foreach时,Task应该捕获AggregateExecption,如下所示:
try
{
Parallel.For(0, _workers.Length, i =>
{
DoWork(i);
});
}
catch (AggregateException ex)
{
// Assume we know what's going on with this particular exception.
// Rethrow anything else. AggregateException.Handle provides
// another way to express this. See later example.
foreach (var e in ex.InnerExceptions)
{
OnLogged(e.Message + e.StackTrace);
}
}