我有一个 WPF C# 应用程序。
通常我使用全局错误处理器来捕获所有的错误:
然而,如果启动一个任务运行一部分代码并且它抛出一个错误,我观察到该错误不会被捕获。
所以我必须加一个“Try-Catch”来捕获它:
"
“我应该怎么做?”
通常我使用全局错误处理器来捕获所有的错误:
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(() =>
{
throw and error here
});
所以我必须加一个“Try-Catch”来捕获它:
Task.Run(() =>
{
try
{
throw an error here
}
catch (Exception ex)
{
do something with error
}
});
"
~ which defeats the object of having a Global Error handler
"的意思是“这就违背了拥有全局错误处理程序的初衷”。“我应该怎么做?”
TaskScheduler.UnobservedTaskException
事件。 - dkozl