首页 > 编程语言 > 详细

WPF异常捕获三种处理 UI线程, 全局异常,Task异常

时间:2018-08-19 14:27:48      阅读:406      评论:0      收藏:0      [点我收藏+]
原文:WPF异常捕获三种处理 UI线程, 全局异常,Task异常

protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
RegisterEvents();
}

private void RegisterEvents()
{
//TaskScheduler.UnobservedTaskException += (sender, args) =>
//{
// MessageBox.Show(args.Exception.Message);
// args.SetObserved();
//};

this.DispatcherUnhandledException += App_DispatcherUnhandledException;
TaskScheduler.UnobservedTaskException += new EventHandler<
UnobservedTaskExceptionEventArgs>(TaskScheduler_UnobservedTaskException);

AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
}

private void App_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
{

}

static void TaskScheduler_UnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e)
{
foreach (Exception item in e.Exception.InnerExceptions)
{
Console.WriteLine("异常类型:{0}{1}来自:{2}{3}异常内容:{4}",
item.GetType(), Environment.NewLine, item.Source,
Environment.NewLine, item.Message);
}
//将异常标识为已经观察到 
e.SetObserved();
}

private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
MessageBox.Show("Unhandled exception.");
}

WPF异常捕获三种处理 UI线程, 全局异常,Task异常

原文:https://www.cnblogs.com/lonelyxmas/p/9501149.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!