管理.NET应用程序以在终止/终止后正常关闭 [英] Manage a .NET app to shutdown gracefully when terminated/killed

查看:115
本文介绍了管理.NET应用程序以在终止/终止后正常关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个.NET控制台应用程序,该应用程序具有许多前台线程. 如果我们使用任务管理器中止进程或发出killjob,在Windows中从命令行中终止,是否可以通过这种方式正常关闭应用程序(在.net控制台应用程序中添加经管理的代码),就像具有功能一样被称为说TodoBeforeShutdown(),它放置对象,关闭所有打开的连接等.

We have a .NET console app that has many foreground threads. If we kill the process using Task Manager or issuing killjob, kill from the command line in windows, is there a way by which we can gracefully shut down the application (adding manged code within the .net console app), something like having a function being called say TodoBeforeShutdown() that disposes objects, closes any open connections, etc.

P.S. -我读了其他线程,它们都提出了不同的方法来杀死进程,而不是我的特定问题,那就是在.NET托管代码中处理终止进程的最佳方法是什么.

P.S. - I read the other threads and they all suggested different ways to kill the process, rather than my specific question, what is the best way we can handle a terminate process, within the .NET managed code.

谢谢.

推荐答案

不幸的是,没有任何事件可以杀死进程.
您可以考虑杀死进程就像切断计算机电源一样,无论您打算在系统关闭时运行什么代码,如果计算机不能正常或正常关闭,该代码也将无法运行.

Unfortunately, there is no event raised that you can handle whenever a process is killed.
You can think of killing a process like cutting off the power to the computer—no matter what code you have designed to run on system shutdown, if the computer doesn't shut down gracefully or properly, that code is not going to run.

使用任务管理器终止进程时,它将调用Win32

When you kill a process using Task Manager, it calls the Win32 TerminateProcess function, which unconditionally forces the process (including all of its owned threads) to exit. The execution of all threads/processes is halted, and all pending I/O requests are canceled. Your program is effectively dead. The TerminateProcess function does not invoke the shutdown sequence provided by the CLR, so your managed app would not even have any idea that is was being shut down.

您建议您担心在应用程序的进程终止时都处理对象,但是这里有两点值得指出:

You suggest that you're concerned about disposing objects whenever your application's process is terminated, but there are a couple of things worth pointing out here:

  • 始终尽力将可能造成的损失降至最低.完成处理后,请尽早处理对象.不要等到以后.在任何给定时间,当程序的进程终止时,您只应保留最少的对象数,这将减少泄漏的可能性.

  • Always strive to minimize the amount of damage that could be done. Dispose of your objects as early as possible, whenever you are finished with them. Don't wait until later. At any given time, when your program's process is terminated, you should only be keeping the bare minimum number of objects around, which will leave fewer possibilities for leaks.

操作系统通常会在终止后清理并释放大多数个资源(即句柄等).

The operating system will generally clean up and free most of these resources (i.e., handles, etc.) upon termination.

最后,不用说,以这种方式终止进程确实是一个例外情况-即使有些资源泄漏,这也是可以预见的.您不应该以这种方式关闭应用程序,而不必杀死所有必要的Windows系统进程(即使以管理员身份运行时也可以).

Finally, it should go without saying that process termination in this way is truly an exceptional condition—even if some resources leak, that's to be expected. You're not supposed to shut an app down this way any more than you're supposed to kill necessary Windows system processes (even though you can when running as an Administrator).

如果这是关闭控制台应用程序的常规计划,则您需要找到另一个计划.

If this is your regular plan to shut down your console application, you need to find another plan.

这篇关于管理.NET应用程序以在终止/终止后正常关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆