更新时间:2023-12-05 12:54:46
这是预期的行为。至少在Windows中, startDetached
等效于使用 DETACHED_PROCESS
标志,其中新进程不会继承其父级的控制台。在其他平台上,该方法也可以执行类似的操作。
It is the expected behavior. At least in Windows startDetached
is equivalent to calling CreateProcess
with the DETACHED_PROCESS
flag, where the new process does not inherit its parent's console. It makes sense that in other platforms the method would do something similar.
在这种情况下,您必须使用 AllocConsole
上的新进程(请注意,您可能需要将流句柄重定向到新控制台),或尝试以其他方式启动该进程(检查 CreateProcess
或 叉子
)。
In this case you'd had to manually allocate a new one using AllocConsole
on the new process (be aware that you may need to redirect the streaming handles to the new console), or try to start the process in a different way (check CreateProcess
or fork
).
BTW,系统
冻结应用程序的原因是因为它是一个同步调用,所以它在其他过程完成之前,不会返回控件。您可以尝试从单独的线程调用 system
,这样可以避免阻塞应用程序的主事件循环。
BTW, the reason system
freezes your application is because it is a synchronous call, so it won't return the control until the other process finishes. You may try calling system
from a separate thread and it this way you avoid blocking the main event loop of your application.