启动进程不是 child
Start a process not as a child
我需要启动一个进程,运行 它作为一个独立的进程。我有某种入门应用程序,其目的是 运行 另一个 exe 并立即退出。实现该目标的最佳方法是什么?
我阅读了 CreateProcess
文档好几遍,但仍有疑问。文档说我需要在完成后调用 CloseHandle
。但是我的 parent 应用程序不应该等待 child 退出。文档的另一部分说我可以单独留下句柄 - 当 parent 进程终止时系统将关闭它们。这是否意味着 child 应用程序在 parent 之后立即退出?这似乎不是真的 - 我关闭了启动程序但我的 child 进程仍然 运行s.
有一个 DETACHED_PROCESS
标志似乎是我要找的东西。但是文档说明了一些关于控制台的内容。什么控制台?我不关心控制台。
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.
好的,那就别等了。您可以在父级中立即调用 CloseHandle
。
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.
如果你不在乎,那就不用担心。
DETACHED_PROCESS flag documentation 状态
For console processes, the new process does not inherit its parent's console (the default)
这意味着:如果您有一个控制台进程并启动了一个新进程,它将不会继承其父级的控制台。
如果您没有控制台进程,则不必担心。
CreateProcess 创建一个子进程,但 不等待子进程完成,所以你已经准备好了。
如果你想等待子进程完成,你应该调用CreateProcess
然后WaitForSingleObject
总结:
// 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
}
请注意,版本 2 不会 终止您的子进程。只有不再需要的资源才会被释放。
我需要启动一个进程,运行 它作为一个独立的进程。我有某种入门应用程序,其目的是 运行 另一个 exe 并立即退出。实现该目标的最佳方法是什么?
我阅读了 CreateProcess
文档好几遍,但仍有疑问。文档说我需要在完成后调用 CloseHandle
。但是我的 parent 应用程序不应该等待 child 退出。文档的另一部分说我可以单独留下句柄 - 当 parent 进程终止时系统将关闭它们。这是否意味着 child 应用程序在 parent 之后立即退出?这似乎不是真的 - 我关闭了启动程序但我的 child 进程仍然 运行s.
有一个 DETACHED_PROCESS
标志似乎是我要找的东西。但是文档说明了一些关于控制台的内容。什么控制台?我不关心控制台。
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.
好的,那就别等了。您可以在父级中立即调用 CloseHandle
。
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.
如果你不在乎,那就不用担心。
DETACHED_PROCESS flag documentation 状态
For console processes, the new process does not inherit its parent's console (the default)
这意味着:如果您有一个控制台进程并启动了一个新进程,它将不会继承其父级的控制台。
如果您没有控制台进程,则不必担心。
CreateProcess 创建一个子进程,但 不等待子进程完成,所以你已经准备好了。
如果你想等待子进程完成,你应该调用CreateProcess
然后WaitForSingleObject
总结:
// 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
}
请注意,版本 2 不会 终止您的子进程。只有不再需要的资源才会被释放。