I need to start a process and run it as a detached process. I have some sort of starter application which purpose is to run another exe and immediately exit. What is a best way to achieve that?
I read CreateProcess
documentation several times but still have questions. Documentation says that I need to call CloseHandle
after I finish. But my parent app shouldn't wait for a child to exit. Another part of documentation says that I can leave handles alone - the system will close them when the parent process terminates. Does that means that child application exits immediately after parent? It seems not true - I close a starter but my child process still runs.
There's a DETACHED_PROCESS
flag that seem what I'm looking for. But documentation says something about console. What console? I don't care about console.
Okay, then don't wait. You can call
CloseHandle
immediately in the parent.No, it doesn't. I'm not sure how you got that from the documentation, but that's not what it means.
If you don't care, then don't worry about it.
DETACHED_PROCESS flag documentation states
that means: if you have a console process and you start a new one, it will not inherit its parent's console.
If you don't have a console process you don't have to worry about it.
CreateProcess creates a child process but does not wait for the child process to finish, so you're already all set.
If you wanted to wait for the child process to complete, you should have called
CreateProcess
and thenWaitForSingleObject
To summarize:
notice that version 2 will not terminate your child process. Only resources which are no longer needed will be released.