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.");
}
上一篇: 不惧复杂光源,美图T9手机KTV实拍评测