我可以通过任务管理器处理我的 Windows 进程的终止吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1527450/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can I handle the killing of my windows process through the Task Manager?
提问by Umair
I have a windows C++ application (app.exe). When the app is closed, I need to perform some cleanup tasks specific to my application. What happens when this process (app.exe) is killed through the Task Manager. Assuming that the application is still responsive, can I somehow handle this situation in my app.exe?
我有一个 Windows C++ 应用程序 (app.exe)。当应用程序关闭时,我需要执行一些特定于我的应用程序的清理任务。当这个进程 (app.exe) 通过任务管理器被终止时会发生什么。假设应用程序仍然响应,我可以在我的 app.exe 中以某种方式处理这种情况吗?
I am looking for something similar to how kill <pid>
in Linux will send the SIGTERM signal to the process indicated by pid. I could then register my own signal handler for SIGTERM and perform the cleanup.
我正在寻找类似于kill <pid>
在 Linux 中如何将 SIGTERM 信号发送到由 pid 指示的进程的东西。然后我可以为 SIGTERM 注册我自己的信号处理程序并执行清理。
回答by Ivan Vu?ica
There are two ways to kill application in Task Manager.
在任务管理器中有两种杀死应用程序的方法。
- Killing through Applicationstab would roughly be equivalent of
SIGTERM
. Application may intercept it and do more processing, since it's basically sending a "close window" message. Message to catch isWM_CLOSE
. - Killing through Processestab would roughly be equivalent of
SIGKILL
. There is nothing you can do to intercept that, short of monitoring user's actions in Task Manager's listbox and End Process button, or having a watchdog process that will see when the first one is killed.
- 通过Applications选项卡杀死大致相当于
SIGTERM
. 应用程序可能会拦截它并进行更多处理,因为它基本上是发送“关闭窗口”消息。要捕获的消息是WM_CLOSE
。 - 通过Processes选项卡杀死大致相当于
SIGKILL
. 除了在任务管理器的列表框和结束进程按钮中监控用户的操作,或者有一个看门狗进程可以看到第一个被杀死的时间,你无法拦截它。
Alternatively, design the application in a way that does not require cleanup, or in a way that it will perform cleanup at startup.
或者,以不需要清理的方式设计应用程序,或者以在启动时执行清理的方式设计应用程序。
回答by Raj More
I think you will need another PID that is monitoring the PID of your app.exe and does the necessary work at the time.
我认为您将需要另一个 PID 来监控您的 app.exe 的 PID 并在当时完成必要的工作。
回答by Mehran
That depends, if the user chooses to "End Task" your application you will be notified and you can handle it see this.
这取决于,如果用户选择“结束任务”您的应用程序,您将收到通知,您可以处理它,请参阅此内容。
but if the user chooses to end the process, you have no way to handle it in your application. the easiest way would be a second process or you can inject into process manager and hook the TerminateProcess API.
但是如果用户选择结束这个过程,你就没有办法在你的应用程序中处理它。最简单的方法是第二个进程,或者您可以注入进程管理器并挂钩 TerminateProcess API。