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.");
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM