为什么 Program.Main 没有出现在崩溃堆栈中

发布于 2024-12-05 04:30:07 字数 816 浏览 2 评论 0原文

我正在编写

一个 C# 应用程序,用于处理主例程中所有未捕获的异常:

    static void Main()
    {
        try
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MainFrame());                     
        }
        catch (Exception e)
        {
            CrashReporter.Report(e);
        }
    }

当我从 Visual Studio 运行该应用程序(甚至发布版本)时,它可以工作。 但是,当我将其发布为单击一次并安装时,在大多数情况下,Main 函数中不会捕获异常。异常堆栈是这样的:

...
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

即 Main 甚至不在堆栈中...... 为什么会发生这种情况? 有什么办法可以做到这种“捕获所有”的事情吗? 为什么发布与本地构建行为不同?


提前致谢 哔叽

everyone

I am writing a C# application that handles all uncaught exceptions in the main routine:

    static void Main()
    {
        try
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MainFrame());                     
        }
        catch (Exception e)
        {
            CrashReporter.Report(e);
        }
    }

And when I am running the application (even Release build) from visual studio it works.
But when I publish it as click-once and install, in most of the cases, the exceptions won't be caught in the Main function. And the exception stack is something like this:

...
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

I.e. Main is not even in the stack...
Why does that happen?
Is there any way to do such 'catch all' thing?
Why is published vs local build behavior different?


Thanks in advance
Serge

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

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

发布评论

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

评论(3

日暮斜阳 2024-12-12 04:30:07

要回答您的有什么方法可以做到这样的“捕获所有”事情吗?问题,请查看AppDomain.UnhandledException

此事件提供未捕获异常的通知。它允许应用程序在系统默认处理程序向用户报告异常并终止应用程序之前记录有关异常的信息。如果可以获得有关应用程序状态的足够信息,则可以采取其他操作,例如保存程序数据以供以后恢复。建议小心,因为如果不处理异常,程序数据可能会损坏。 ...

和 ​​Application.ThreadException事件。

此事件允许 Windows 窗体应用程序处理 Windows 窗体线程中发生的未处理的异常。将事件处理程序附加到 ThreadException 事件来处理这些异常,这将使您的应用程序处于未知状态。如果可能,异常应该由结构化异常处理块来处理。 ...

To answer your Is there any way to do such 'catch all' thing? question, check out AppDomain.UnhandledException

This event provides notification of uncaught exceptions. It allows the application to log information about the exception before the system default handler reports the exception to the user and terminates the application. If sufficient information about the state of the application is available, other actions may be undertaken — such as saving program data for later recovery. Caution is advised, because program data can become corrupted when exceptions are not handled. ...

and Application.ThreadException events.

This event allows your Windows Forms application to handle otherwise unhandled exceptions that occur in Windows Forms threads. Attach your event handlers to the ThreadException event to deal with these exceptions, which will leave your application in an unknown state. Where possible, exceptions should be handled by a structured exception handling block. ...

演多会厌 2024-12-12 04:30:07

我希望主线程不会引发异常。

显然,其他线程在堆栈上没有 Main (因为线程函数是它们的入口点)。

当您收到“nativewindow”回调时,这意味着本机窗口正在不同的线程上运行。如果您需要代码在一个线程上运行,您可能需要使用消息传递或使用“Invoke”来中继回调。

I expect that the exception wasn't thrown on the main thread.

Obviously, the other threads don't have Main on the stack (because the thread function is their entry point).

As you are getting a 'nativewindow' callback, this means that the native window is operating on a different thread. You may want to use message passing or use 'Invoke' to relay the callback if you need your code to operate on one thread.

自由范儿 2024-12-12 04:30:07

异常是在窗口回调中抛出的;这些在事件分派线程上调用。您看到的堆栈跟踪是来自该线程的堆栈跟踪,而不是程序启动时的主堆栈跟踪。

The exception is being thrown in a window callback; those are invoked on the event-dispatch thread. The stack trace you see is the one from that thread, not the main one where your program starts up.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文