我可以通过任务管理器处理 Windows 进程的终止吗?
我有一个 Windows C++ 应用程序 (app.exe)。当应用程序关闭时,我需要执行一些特定于我的应用程序的清理任务。通过任务管理器终止此进程 (app.exe) 时会发生什么。假设应用程序仍然响应,我可以以某种方式在 app.exe 中处理这种情况吗?
我正在寻找类似于 Linux 中的 kill
如何将 SIGTERM 信号发送到 pid 指示的进程的东西。然后我可以为 SIGTERM 注册我自己的信号处理程序并执行清理。
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?
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
有两种方法可以在任务管理器中终止应用程序。
SIGTERM
。应用程序可能会拦截它并进行更多处理,因为它基本上是发送“关闭窗口”消息。要捕获的消息是WM_CLOSE
。SIGKILL
。除了在任务管理器的列表框和“结束进程”按钮中监视用户的操作,或者使用一个看门狗进程来查看第一个进程何时被终止之外,您无法拦截该行为。或者,以不需要清理的方式设计应用程序,或者以在启动时执行清理的方式设计应用程序。
There are two ways to kill application in Task Manager.
SIGTERM
. Application may intercept it and do more processing, since it's basically sending a "close window" message. Message to catch isWM_CLOSE
.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.Alternatively, design the application in a way that does not require cleanup, or in a way that it will perform cleanup at startup.
我认为您将需要另一个 PID 来监视您的 app.exe 的 PID 并在当时执行必要的工作。
I think you will need another PID that is monitoring the PID of your app.exe and does the necessary work at the time.
这取决于,如果用户选择“结束任务”您的应用程序,您将收到通知,并且您可以处理它 看到这个。
但是如果用户选择结束该进程,您就无法在应用程序中处理它。最简单的方法是第二个进程,或者您可以注入进程管理器并挂钩 TerminateProcess API。
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.