Windows 窗体未处理异常对话框
每当我的 C# 应用程序遇到 UE 时,我希望获得默认 Windows 窗体未处理异常对话框。 在 vs 2005 中,当我在 app.conf 中关闭 jit 调试时,如下所示:
<configuration>
<system.windows.forms jitDebugging="false" />
<configuration>
应用程序行为正确并显示 Windows Forms UE 默认对话框,其中包含继续、退出、调用堆栈等。
然而,在 vs 2008 中,在同一台计算机或不同计算机上,即使我禁用 jit,我仍然会收到默认 .NET 未处理异常对话框,其中包含“调试”、“发送报告”和“不发送”按钮。
如何使我的 vs 2008 应用程序像我在 vs 2005 中制作的应用程序一样,显示 Windows Forms UE 对话框?
而建议使用
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
请不要仅仅因为我在 vs 2005 项目中不使用自定义处理程序 ,为什么要在 vs 2008 中使用?我想让CLR来做这个工作。
任何帮助表示赞赏
I want to get Default Windows Forms Unhandled-Exception Dialog whenever my C# application encounters U-E.
In vs 2005 when I turn off jit Debugging in app.conf like this:
<configuration>
<system.windows.forms jitDebugging="false" />
<configuration>
the application behaves correctly and shows Windows Forms U-E default dialog, with Continue, Quit, call stack and all.
However in vs 2008, on the same machine or different, even though I diable jit I still get Default .NET Unhandled-Exception Dialog, with Debug, Send Report and Don't Send buttons.
How can I make my vs 2008 app act like the one I make in vs 2005, to show Windows Forms U-E dialog box?
Please do not recommend to use
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
just because I don't use custom handler in my vs 2005 project, why would I use in vs 2008? I want to let this job do CLR.
Any help is appreciated
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在谈论不同的异常处理功能。您在“退出并继续”中看到的 ThreadExceptionDialog按钮由 Application.ThreadException 事件。仅当异常发生在 UI 线程上、响应 Windows 消息而运行的事件处理程序抛出异常时,它才会出现。然而,工作线程中的任何异常都会通过 AppDomain.UnhandledException 轰炸程序。
您无法处理后者,程序已死并且无法继续。显示 ThreadExceptionDialog 是没有意义的。
您可以通过调用 Application.SetUnhandledExceptionMode() 禁用 ThreadException 事件来使程序始终炸弹。现在,每个未处理的异常都会触发 AppDomain.UnhandledException 并终止程序。这可能不是您想要的,但却是明智的选择。
另请注意,当您使用调试器运行程序时,ThreadException 事件将被禁用。这大概就是您在 VS2008 中看到差异的原因。除此之外没有任何变化。
You are talking about different exception handling features. The ThreadExceptionDialog you see with the Quit and Continue buttons is triggered by the Application.ThreadException event. It will only ever appear if the exception happens on the UI thread, when an event handler that runs in response to a Windows message throws an exception. Any exception in a worker thread however will bomb the program through AppDomain.UnhandledException.
You cannot handle the latter, the program is dead and cannot continue. Displaying ThreadExceptionDialog is pointless.
You can make the program bomb consistently by disabling the ThreadException event by calling Application.SetUnhandledExceptionMode(). Now every unhandled exception will trigger AppDomain.UnhandledException and terminate the program. That's probably not what you want but is the wise choice.
Also note that the ThreadException event is disabled when you run the program with a debugger. Which is presumably why you see a difference in VS2008. There have otherwise not been any changes.
汉斯·帕桑特的回答似乎很合理。但是,即使您说您不想,我仍然推荐 AppDomain 上的处理程序.CurrentDomain.UnhandledException 确保您确定应用程序意外崩溃时会发生什么情况。
Answer by Hans Passant seems sensible. However, even though you say you don't want to I still recommend a handler on
AppDomain.CurrentDomain.UnhandledException
to make sure you determine what happens when your application crashes unexpectedly.