我需要捕获子进程的进程终止信号。
因此,例如,如果我的Win32控制台应用程序产生记事本进程并且用户关闭记事本,我想检测到它。
我不想阻止(异步模型)
我正在使用win api CreateProcess
创建一个流程答案 0 :(得分:3)
您是否尝试WaitForSingleObject()
dwMilliseconds
参数为0?如果WaitForSingleObject()
为0,dwMilliseconds
将立即返回,如果流程尚未结束,则会返回WAIT_TIMEOUT
;如果流程尚未结束,则会返回WAIT_OBJECT_0
。
例如,假设子进程句柄为hProcess
:
DWORD result = WaitForSingleObject(hProcess, 0);
if (result == WAIT_TIMEOUT)
/* Process not dead */;
else if (result == WAIT_OBJECT_0)
/* Process dead */;
else
/* Error occured */;
另一种选择是GetExitCodeProcess()
。如果它仍然在运行,它返回的“退出代码”将是STILL_ACTIVE
,否则它将返回实际的退出代码。
示例,再次假设子进程句柄为hProcess
:
DWORD exitCode;
if (!GetExitCodeProcess(hProcess, &exitCode))
/* Error occured */;
else if (exitCode == STILL_ACTIVE)
/* Process is still running */
else
/* exitCode now contains the process exit code, and the process is not running anymore */;
这两个例子都是非阻塞的
答案 1 :(得分:2)