在运行过程中捕获Windows终端关闭 [英] Catch Windows terminal closing on running process

查看:114
本文介绍了在运行过程中捕获Windows终端关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在命令提示符窗口中,我正在运行一个进程。在该过程仍在执行的同时,我点击了命令提示符窗口一角的(红色) 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?

推荐答案

通过您在 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屋!

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