I have a program which needs to invoke a process to perform an operation and wait for it to complete the operation. The problem is that the invoked process clones itself and exits, which causes the wait api to return when the process exits. How can I wait for the cloned process to finish execution and return?
I am using the windows JOB object as mentioned in http://www.microsoft.com/msj/0399/jobkernelobj/jobkernelobj.aspx, But I am not sure if this is the best way.
+1 for using job objects 😉
Assuming the process that you’re running isn’t spawning the cloned version of itself in such a way that it breaks out of the job…
You should be able to simply monitor the job events and act on
JOB_OBJECT_MSG_ACTIVE_PROCESS_ZERO(see JOBOBJECT_ASSOCIATE_COMPLETION_PORT and SetInformationJobObject()). Monitoring the job in this way will also give you notifications of the processId’s of new processes created within the job and details of when they exit.