从另一个线程捕获 Application.ThreadException 中的错误

发布于 2025-01-08 02:10:57 字数 1187 浏览 0 评论 0原文

我有一个来自应用程序的窗口,其program.cs代码如下:

static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.ThreadException += (sndr, evnt) => 
        { 
            MessageBox.Show("An error occur. Please contact your administrator.\nError description: " + 
                evnt.Exception.Message + "\nStack trace:\n" + evnt.Exception.StackTrace, 
                evnt.Exception.GetType().ToString(), MessageBoxButtons.OK, MessageBoxIcon.Error); 
        };
        Application.Run(new frmLogin());
    }
}

但是当在 BackgroundWorker.DoWork 事件中引发异常时,Application.ThreadException 没有捕获它。

如何 Application.ThreadException 捕获由 BackgroundWorker创建的不同线程的异常?< /strong>

我知道在 Application.ThreadException 中捕获错误不是一个好的做法,但我们仅将其用作最后一级的错误捕获。

我的代码是用 C# 编写的,框架4,在VS2010 Pro中构建。

请帮忙。提前致谢。

I have a windows from application with a program.cs code like this:

static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.ThreadException += (sndr, evnt) => 
        { 
            MessageBox.Show("An error occur. Please contact your administrator.\nError description: " + 
                evnt.Exception.Message + "\nStack trace:\n" + evnt.Exception.StackTrace, 
                evnt.Exception.GetType().ToString(), MessageBoxButtons.OK, MessageBoxIcon.Error); 
        };
        Application.Run(new frmLogin());
    }
}

But when an exception is thrown inside the BackgroundWorker.DoWork event, the Application.ThreadException didn't catch it.

How can the Application.ThreadException catch an exception from a different thread created by a BackgroundWorker?

I know catching errors in the Application.ThreadException is not a good practice but we only use it as our last level of error catching.

My code is in C#, framework 4, build in VS2010 Pro.

Please help. Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

诺曦 2025-01-15 02:10:57

请参阅此问题:BackgroundWorker 异常不会像这样传播,因此您无法捕获它们在 ThreadException 中。

抛出异常的BackgroundWorkers照常调用RunWorkerCompleted,但在事件参数上设置Error属性。

See this question: BackgroundWorker exceptions don't get propagated like that, so you can't catch them in ThreadException.

BackgroundWorkers that throw exceptions call RunWorkerCompleted as usual, but set the Error property on the event arguments.

拿命拼未来 2025-01-15 02:10:57

BackgroundWorker 为您捕获异常(不会暴露给 Application.ThreadException 或 AppDomain.UnhandledException)并将异常传递给 RunWorkerCompleted 事件的参数。您将在 RunWorkerCompleted 事件处理程序上处理异常。

一旦 RunWorkerCompleted 事件触发,您可以得到 RunWorkerCompletedEventArgs.Error 异常。

    private void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        if (e.Error != null)
        { 
            // handle exception here
        }
    }

The BackgroundWorker catches exception for you(will not expose to Application.ThreadException or AppDomain.UnhandledException) and pass the exception to argument of RunWorkerCompleted event. You will handle the exception on RunWorkerCompleted event handler.

There is RunWorkerCompletedEventArgs.Error that you can get the exception once the RunWorkerCompleted event fired.

    private void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        if (e.Error != null)
        { 
            // handle exception here
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文