我有一个wpf c#应用程序。Task.Run中的全局错误处理
我通常使用一个全局错误处理程序捕获所有的错误:
private void Application_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
try
{
Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() => Xceed.Wpf.Toolkit.MessageBox.Show(e.Exception.ToString(), "Error",
MessageBoxButton.OK, MessageBoxImage.Error)));
e.Handled = true;
InformedWorkerDataService.Common.Shared.RecordMessage(e.Exception.ToString(), true);
}
finally { }
}
但是,如果开始一个task.run“的码位”,然后抛出一个错误我观察到,误差不抓:
Task.Run(() =>
{
throw and error here
});
,所以我必须在把“尝试捕获”的事情捕捉到它:
Task.Run(() =>
{
try
{
throw an error here
}
catch (Exception ex)
{
do something with error
}
});
〜这防卫厅具有全局错误处理程序的对象
我该怎么办?
试试['TaskScheduler.UnobservedTaskException'](https://msdn.microsoft.com/ en-us/library/system.threading.tasks.taskscheduler.unobservedtaskexception(v = vs.110).aspx)event – dkozl
@dkozl cool - thanks :) –