Windows Forms Unhandled-Exception Dialog

自闭症网瘾萝莉.ら 提交于 2020-07-05 03:41:05

问题


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


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/2629306/windows-forms-unhandled-exception-dialog

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!