开始一个过程,而不是像孩子一样 [英] Start a process not as a child

查看:25
本文介绍了开始一个过程,而不是像孩子一样的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要启动一个进程并将其作为一个分离的进程运行.我有某种入门应用程序,其目的是运行另一个 exe 并立即退出.实现这一目标的最佳方法是什么?

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?

我阅读了CreateProcess 文档多次但仍有问题.文档说我需要在完成后调用 CloseHandle.但是我的父应用程序不应该等待孩子退出.文档的另一部分说我可以不理会句柄——当父进程终止时,系统将关闭它们.这是否意味着子应用程序在父应用程序之后立即退出?这似乎不是真的 - 我关闭了一个启动器,但我的子进程仍在运行.

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.

有一个 DETACHED_PROCESS 标志,这似乎是我正在寻找的.但是文档说明了一些关于控制台的内容.什么控制台?我不在乎控制台.

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 标志文档 状态

对于控制台进程,新进程不继承其父进程的控制台(默认)

这意味着:如果你有一个控制台进程并且你开始一个新的进程,它不会继承其父进程的控制台.

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创建一个子进程但不等待子进程完成,所以你已经准备好了.

CreateProcess creates a child process but does not wait for the child process to finish, so you're already all set.

如果你想等待子进程完成,你应该先调用CreateProcess然后WaitForSingleObject

If you wanted to wait for the child process to complete, you should have called CreateProcess and then 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 版将不会终止您的子进程.只有不再需要的资源才会被释放.

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

这篇关于开始一个过程,而不是像孩子一样的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆