Start a process not as a child

十年热恋 提交于 2019-12-10 14:23:39

问题


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.


回答1:


DETACHED_PROCESS flag documentation states

For console processes, the new process does not inherit its parent's console (the default)

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 then WaitForSingleObject

To summarize:

// Version 1) Launch and wait for a child process completion
STARTUPINFO info = { sizeof(info) };
PROCESS_INFORMATION processInfo;
if (CreateProcess(L"C:\\myapp.exe", L"", NULL, NULL, TRUE, 0, NULL, NULL, &info, &processInfo)) {
    ::WaitForSingleObject(processInfo.hProcess, INFINITE); // DO WAIT for the child to exit
    CloseHandle(processInfo.hProcess);
    CloseHandle(processInfo.hThread);
}

// ----------------------------------------------------------------

// Version 2) Launch and do NOT wait for a child process completion
STARTUPINFO info = { sizeof(info) };
PROCESS_INFORMATION processInfo;
if (CreateProcess(L"C:\\myapp.exe", L"", NULL, NULL, TRUE, 0, NULL, NULL, &info, &processInfo)) {
    CloseHandle(processInfo.hProcess); // Cleanup since you don't need this
    CloseHandle(processInfo.hThread); // Cleanup since you don't need this
}

notice that version 2 will not terminate your child process. Only resources which are no longer needed will be released.




回答2:


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.

Okay, then don't wait. You can call CloseHandle immediately in the parent.

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.

No, it doesn't. I'm not sure how you got that from the documentation, but that's not what it means.

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.

If you don't care, then don't worry about it.



来源:https://stackoverflow.com/questions/29899461/start-a-process-not-as-a-child

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!