简体   繁体   English

捕获异常并在崩溃时正确处理程序

[英]Catch exception and properly handle program when it crashes

AppDomain.CurrentDomain.UnhandledException += 
    new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
Forms.Application.ThreadException += 
    new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);

was the code i snipped off a similar question, but it fails to solve my case. 是我删掉了类似问题的代码,但无法解决我的问题。 What i'm trying to do is catch a crash and dispose of my application correctly so i don't leave behind memory dumps/traces/systray icons 我正在尝试做的是崩溃并正确处理我的应用程序,这样我就不会留下内存转储/跟踪/系统托盘图标

If you terminate the process using debugger or task manager, there's no way to handle this situation -- be it a .NET application or a native one. 如果使用调试器或任务管理器终止该过程,则无法处理这种情况-它是.NET应用程序还是本机应用程序。

Handling OS exceptions in CLR may be possible, but it is not advised. 可以在CLR中处理OS异常,但不建议这样做。 See Is it possible to catch an access violation exception in .NET? 请参阅是否可以在.NET中捕获访问冲突异常? for more details. 更多细节。

In almost all cases, your process should not be terminated in these ways, and even if it does, proper disposing is unnecessary, as OS will properly close almost all handles: files, connections etc. Terminated applications leaving tray icons behind is a 20-year old bug in Explorer and you can't do anything about it. 在几乎所有情况下,都不应以这些方式终止您的进程,即使这样做也没有必要进行适当的处​​理,因为OS会正确关闭几乎所有句柄:文件,连接等。终止的应用程序在其后留下托盘图标是20- Explorer中一个古老的错误,您对此无能为力。 Let's just hope Microsoft will fix it one day. 我们只是希望微软有一天能解决它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM