在 Windows 中等待孙子进程 [英] Waiting for grandchild processes in windows
问题描述
是否可以在 Windows 中等待子进程启动的所有进程?我无法修改子进程或孙进程.
Is it possible to wait for all processes launched by a child process in Windows? I can't modify the child or grandchild processes.
具体来说,这就是我想要做的.我的进程启动uninstallA.exe.uninistallA.exe进程启动uninstallB.exe并立即退出,uninstallB.exe运行一段时间.我想等待uninstallB.exe退出,以便知道卸载何时完成.
Specifically, here's what I want to do. My process launches uninstallA.exe. The process uninistallA.exe launches uninstallB.exe and immediately exits, and uninstallB.exe runs for a while. I'd like to wait for uninstallB.exe to exit so that I can know when the uninstall is finished.
推荐答案
使用 CreateJobObject
创建作业对象.使用 CreateProcess
以暂停状态启动 UninstallA.exe
.使用 AssignProcessToJobObject
将该新流程分配给您的作业对象.通过对从 CreateProcess
返回的线程句柄调用 ResumeThread
来启动 UninstallA.exe 运行.
Create a Job Object with CreateJobObject
. Use CreateProcess
to start UninstallA.exe
in a suspended state. Assign that new process to your job object with AssignProcessToJobObject
. Start UninstallA.exe running by calling ResumeThread
on the handle of the thread you got back from CreateProcess
.
然后是困难的部分:等待作业对象完成其执行.不幸的是,这比任何人合理希望的要复杂得多.基本思路是先创建一个 I/O 完成端口,然后创建对象对象,将其与 I/O 完成端口关联,最后等待 I/O 完成端口(通过 GetQueuedCompletionStatus 获取其状态)
).Raymond Chen 在他的 博客上进行了演示(并解释了这是如何发生的).
Then the hard part: wait for the job object to complete its execution. Unfortunately, this is quite a bit more complex than anybody would reasonably hope for. The basic idea is that you create an I/O completion port, then you create the object object, associate it with the I/O completion port, and finally wait on the I/O completion port (getting its status with GetQueuedCompletionStatus
). Raymond Chen has a demonstration (and explanation of how this came about) on his blog.
这篇关于在 Windows 中等待孙子进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!