【发布时间】:2015-02-08 20:32:32
【问题描述】:
我正在使用 .NET 3.5 的任务并行库(NuGet package)。
运行以下代码时,OnlyOnFaulted 任务不会在MethodThrowsException() 引发异常时运行:
var task = Task.Factory.StartNew<SomeType>(() =>
{
MethodThrowsException();
})
.ContinueWith(t =>
{
ExceptionMessageTextBox.Text = t.Exception.Message;
},
CancellationToken.None, TaskContinuationOptions.OnlyOnFaulted, TaskScheduler.FromCurrentSynchronizationContext());
我(最终)得到了这个异常:
System.AggregateException was unhandled
Message: An unhandled exception of type 'System.AggregateException' occurred in System.Threading.dll
Additional information: TaskExceptionHolder_UnhandledException
我看不到它在 System.Threading.dll 中的位置(我需要加载 pdb)。
据我所知,我正确地观察到了 this MSDN article 指定的异常。
【问题讨论】:
-
您是如何遇到异常的?这是用调试器运行的吗?
-
@theMayer:Visual Studio 2013 将异常报告为 System.Threading.dll 中未处理。添加截图。
-
您是否打开了“仅我的代码”?如果是这样,它会将异常报告为“未处理”,因为它们不是由“您的代码”处理的。关闭此功能——它没有用。
-
您确定不是继续导致问题吗? IE。在 ContinueWith 中的代码周围放置一个 try/catch。你能看到 AggregateException 的 InnerException 是什么吗?
-
@MattSmith:100% 正确。 ContinueWith 未在我的 UI 线程上运行,因此设置控件属性引发异常。我无法让它工作,所以我只是使用 Dispatch(呃)。谢谢
标签: c# .net parallel-processing .net-3.5 task-parallel-library