在WPF中的子线程中捕获未处理的异常 [英] Catching Unhandled Exceptions in Child Threads in WPF
本文介绍了在WPF中的子线程中捕获未处理的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
示例代码:
private void Application_DispatcherUnhandledException(object sender,System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
MessageBox.Show(details error message);
}
private void Application_Startup(object sender,StartupEventArgs e)
{
// ...
//如果在此抛出异常,它被处理
// ...
Thread [] threads = new Thread [numThreads]; (int i = 0; i< numThreads; i ++)
{
threads [i] = new Thread(doWork);
threads [i] .Start();
}
}
private void doWork()
{
// ...
//此处抛出的异常未处理
// ...
}
编辑: strong>一旦发生未处理的异常,我想显示一个包含堆栈跟踪的错误消息,然后退出应用程序。
解决方案
尝试连接到 AppDomain.CurrentDomain.UnhandledException 事件也是如此。
I have a WPF application that spins off several threads. I have defined a DispatcherUnhandledException event handler in App.xaml.cs that displays a detailed error message, and this handler gets called every time the UI thread encounters an exception. The problem is with the child threads: their unhandled exceptions never get handled. How do I do this?
Sample code:
private void Application_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
MessageBox.Show("detailed error message");
}
private void Application_Startup(object sender, StartupEventArgs e)
{
//...
//If an Exception is thrown here, it is handled
//...
Thread[] threads = new Thread[numThreads];
for(int i = 0; i < numThreads; i++)
{
threads[i] = new Thread(doWork);
threads[i].Start();
}
}
private void doWork()
{
//...
//Exception thrown here and is NOT handled
//...
}
Edit: Once an unhandled exception occurs, I want to display an error message with a stack trace, and then exit the application.
解决方案
Try hooking up to the AppDomain.CurrentDomain.UnhandledException event as well.
这篇关于在WPF中的子线程中捕获未处理的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文