Windows 8 在非UI线程上处理错误
由于某些原因,在非UI线程发生的未处理异常不会由App_UnhandledException处理程序处理 这种方法适用于Windows Phone应用程序全局处理、跟踪和分析异常,但不适用于Windows 8应用程序Windows 8 在非UI线程上处理错误,windows-8,microsoft-metro,Windows 8,Microsoft Metro,由于某些原因,在非UI线程发生的未处理异常不会由App_UnhandledException处理程序处理 这种方法适用于Windows Phone应用程序全局处理、跟踪和分析异常,但不适用于Windows 8应用程序 this.UnhandledException += App_UnhandledException; //doesn't handle private void Button_Click_1(object sender, RoutedEventArgs e) { var
this.UnhandledException += App_UnhandledException; //doesn't handle
private void Button_Click_1(object sender, RoutedEventArgs e)
{
var task = new Task(() => { throw new NullReferenceException("Test exc in UI thread"); });
task.Start();
}
请告知。使用新的
异步
/等待
关键字:
private async void按钮\u单击\u 1(对象发送方,RoutedEventArgs e)
{
var task=new task(()=>{抛出新的NullReferenceException(“UI线程中的测试exc”);});
task.Start();
尝试
{
等待任务;
}
捕获(例外情况除外)
{
var msg=new MessageDialog(例如ToString(),“发生错误”);
等待msg.ShowAsync();
}
}
仅使用任务
方法:
private void按钮\u单击\u 1(对象发送者,路由目标)
{
var task=new task(()=>{抛出新的NullReferenceException(“UI线程中的测试exc”);});
task.ContinueWith(t=>
{
var msg=newmessagedialog(t.Exception.ToString(),“发生错误”);
msg.ShowAsync().Start();
},TaskContinuationOptions.OnlyOnFaulted);
task.Start();
}
要捕获所有未处理的异常,请参见以下问题:
任务调度程序。从CurrentSynchronizationContext
到ContinueWith
。非常感谢!这正是我需要的