How to determine when spawned process is ready? (Using CreateProcess() and FindWindow())

江枫思渺然 提交于 2019-12-01 16:18:35
gimpf

(Edit): User IInspectable pointed out problems with WaitForInputIdle(), and suggested CBT Hooks instead.

(...) callback function used with the SetWindowsHookEx function. The system calls this function before activating, creating, (...) a window; (... many other things).

Also, CBT is short for computer-based training, for whatever reason.

(Old, beware, see comments.) You are looking for WaitForInputIdle(). Quote:

When a parent process creates a child process, the CreateProcess function returns without waiting for the child process to finish its initialization. Before trying to communicate with the child process, the parent process can use the WaitForInputIdle function to determine when the child's initialization has been completed.

Have you looked at WaitForInputIdle?

If the process you're starting is one you can change, have it send a message back to the parent when it is ready. You can pass the HWND of the parent as a command line parameter, or use FindWindow if you can guarantee that the parent will be unique.

I assume that the source code of both processes is under your control.

  • You can let the second process post a message to the first when it's ready, if the second knows the needed details of the first process' message window.
  • Or you can wait in the first process for an agreed-upon named synchronization object, like event or mutex, being set from the second process.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!