在醒目的WPF子线程未处理的异常 [英] Catching Unhandled Exceptions in Child Threads in WPF

查看:274
本文介绍了在醒目的WPF子线程未处理的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WPF应用程序,旋转了多个线程。我已经定义在App.xaml.cs一个DispatcherUnhandledException事件处理程序,显示详细的错误信息,并且此处理被调用每一个UI线程遇到异常的时间。问题是与子线程:其未处理的异常永远不会得到处理。我如何做到这一点。

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?

示例代码:

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
    //...
}

修改一旦未处理的异常,我想显示一个堆栈跟踪的错误信息,然后退出应用程序。

Once an unhandled exception occurs, I want to display an error message with a stack trace, and then exit the application.

推荐答案

尝试挂钩到 AppDomain.CurrentDomain.UnhandledException 事件也是如此。

Try hooking up to the AppDomain.CurrentDomain.UnhandledException event as well.

这篇关于在醒目的WPF子线程未处理的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆