捕获正在运行的进程关闭的 Windows 终端 [英] Catch Windows terminal closing on running process
问题描述
在命令提示符窗口中,我有一个正在运行的进程.当进程仍在执行时,我单击命令提示符窗口角落中的(红色)X".命令提示符窗口关闭,正在运行的进程终止.
In a command prompt window, I have a running process. While the process is still executing, I click the (red) 'X' in the corner of the command prompt window. The command prompt window closes, and the running process is terminated.
在 Linux 上,关闭正在运行的进程的父终端将发送该进程 SIGHUP
.如何在 Windows 上捕获此事件?
On Linux, closing the parent terminal of a running process will send that process SIGHUP
. How do I catch this event on Windows?
推荐答案
SIGHUP 的等效项是通过您在 SetConsoleCtrlHandler.您的回调函数将在任意线程池线程上使用 dwCtrlType = CTRL_CLOSE_EVENT 调用.您有 5 秒的时间清理,您无法取消关闭.
The equivalent of SIGHUP is provided through the callback you register with SetConsoleCtrlHandler. Your callback function will be called on an arbitrary threadpool thread with dwCtrlType = CTRL_CLOSE_EVENT. You've got 5 seconds to clean-up, you cannot cancel the close.
示例代码位于 这篇 MSDN 文章
这篇关于捕获正在运行的进程关闭的 Windows 终端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!