简体   繁体   English

分离已启动的进程

[英]Detaching a started process

I've started a process using QProcess::start() and I need to detach it afterwards. 我已经使用QProcess::start()启动了一个进程,然后我需要将其分离。 How can I do it? 我该怎么做? I haven't found relevant info in the Qt docs. 我没有在Qt文档中找到相关信息。

I'm aware of QProcess::startDetached() , but due to other code in the program, I can't use it (I need to separate the starting and the detaching of the process). 我知道QProcess::startDetached() ,但是由于程序中的其他代码,我无法使用它(我需要分离进程的启动和分离)。

If you take a look into the implementation of QProcess::~QProcess() , you will know how QProcess terminates the process with its destruction. 如果您查看QProcess::~QProcess() ,您将了解QProcess如何通过其销毁来终止该进程。 Also, note that QProcess::setProcessState() is protected , which means you could implement a QDetachableProcess inherited from QProcess with a method detach() to call setProcessState(QProcess::NotRunning); 另请注意, QProcess::setProcessState()受保护 ,这意味着您可以使用方法detach()实现从QProcess继承的QDetachableProcess以调用setProcessState(QProcess::NotRunning); as a workaround. 作为一种解决方法。

For example: 例如:

class QDetachableProcess : public QProcess
{
public:
    QDetachableProcess(QObject *parent = 0) : QProcess(parent){}
    void detach()
    {
        this->waitForStarted();
        setProcessState(QProcess::NotRunning);
    }
};

Then you could do things like this: 然后你可以做这样的事情:

QDetachableProcess process;
process.setEnvironment(QStringList() << "SOME_ENV=Value");

process.start();

process.detach();

You can't as of 5.1, see here . 你不能在5.1, 见这里 There's also a suggestion in the comments, not sure if useful for your case): 评论中还有一个建议,不确定是否对您的案例有用):

Workaround proposal: write a helper process that starts detached processes, and terminates itself when all setting up is completed. 变通方法建议:编写一个启动分离进程的帮助程序进程,并在完成所有设置后终止自身。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM