为什么 Program.Main 没有出现在崩溃堆栈中
我正在编写
一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
要回答您的有什么方法可以做到这样的“捕获所有”事情吗?问题,请查看AppDomain.UnhandledException
和 Application.ThreadException事件。
To answer your Is there any way to do such 'catch all' thing? question, check out AppDomain.UnhandledException
and Application.ThreadException events.
我希望主线程不会引发异常。
显然,其他线程在堆栈上没有 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.
异常是在窗口回调中抛出的;这些在事件分派线程上调用。您看到的堆栈跟踪是来自该线程的堆栈跟踪,而不是程序启动时的主堆栈跟踪。
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.