1

There is a recommendation to associate a Job Object with I/O completion port and to listen for notifications JOB_OBJECT_MSG_ACTIVE_PROCESS_ZERO as a right way to wait for child process (without access to source code) tree completion. But in MSDN:

Note that, except for limits set with the JobObjectNotificationLimitInformation information class, messages are intended only as notifications and their delivery to the completion port is not guaranteed. The failure of a message to arrive at the completion port does not necessarily mean that the event did not occur.

So there is no really reliable way to wait for the process tree completion in Windows, isn't it?

SerG
  • 1,251
  • 4
  • 18
  • 37
  • FWIW, I use this approach routinely and I've never yet seen it fail. So either it doesn't really happen (at least not to that particular notification) or it fails only very very rarely. If you wanted to cover this possibility as a precaution, you could periodically check the number of processes in the job. – Harry Johnston Jan 20 '15 at 04:40
  • Or, if polling isn't acceptable, you could retrieve the list of processes in the job, choose one at random, wait for it to exit, and then repeat. (Avoiding the obvious race condition would be a bit fiddly, but not too bad.) – Harry Johnston Jan 20 '15 at 04:42
  • @HarryJohnston I also noticed the problem of forestalling the message: right after I have received `JOB_OBJECT_MSG_ACTIVE_PROCESS_ZERO` C# static function `Process.GetProcessesByName` still returns some objects (even after `QueryInformationJobObject` have returned 0 assigned processes). Little pause before the call could solve the problem but it's not deterministic way. If the child process spawn <5 processes 10 ms Thread.Sleap is enough, but in case of thousand processes it's needed more (I use 100 ms). – SerG Jun 14 '15 at 16:57
  • I would expect that for most purposes, the fact that the process object may still exist (and be visible in process lists) after the process has exited (as seen by the job object) wouldn't matter. In particular, I believe it is safe to assume at that point that there are no running threads, or at least none that are running user-mode code. – Harry Johnston Jun 14 '15 at 23:41

0 Answers0