确保QProcess在其父QThread终止时终止 [英] Ensuring QProcess termination on termination of its parent QThread

查看:532
本文介绍了确保QProcess在其父QThread终止时终止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Qt内编写代码,其中我使用多个线程(Qthreads)以下面的代码段所示的方式启动命令行进程:

I am writing a code within Qt where I am using multiple threads (Qthreads) to launch command-line processes in a manner shown in the code snippet below:

void test_streamer_thread::run()
{
    QProcess    start_process;
    ...

    ret_status = start_process.execute("some_cmd.exe",some_args);
    start_process.close();
}

启动后,该进程将无限进行(在Windows任务管理器中显示为单独的进程).但是,当应用程序终止时,该过程仍然继续存在.如何确保此过程在启动它的应用程序终止时终止.

Once launched, the process goes on infinitely (shows up as a separate process in the windows task manager). However, when the application terminates, the process still continues to exist. How do I ensure that this process terminates on termination of the application launching it.

推荐答案

完全不需要使用单独的线程来启动进程.

Using separate threads to launch processes is entirely unnecessary.

确保子进程在应用程序终止时终止的最简单方法是

The simplest way to ensure that child processes are terminated when the application terminates would be

QProcess * p = new QProcess(....);
connect(qApp, SIGNAL(aboutToQuit()), process, SLOT(terminate()));

有关完整示例,请参见下文.

See below for a complete example.

这种误解就像疾病一样蔓延开来,线程是解决每个人问题的方法.我的观察结果是,在10个带有Qt标签的帖子中,有9个帖子中不需要使用线程,这是由于不了解问题的结果.

There's this misunderstadning that is spreading like a disease that threads are the solution to everyone's problems. My observation is that in 9 out of 10 posts with the Qt tag, the use of threads is unnecessary and is a result of not understanding the problem.

我的规则是:如果您认为需要使用线程,请尝试解释一下(如果只是在您的脑海中),以说服您的同事.完成此操作后,请检查您引用来支持您的论点的所有事实是否都是真实的.在许多情况下不是.

My rule is: if you think you need to use threads, try explaining it, if only in your mind, to convince your coworker. After doing this, check that all the facts you cited to back your argument are in fact true. In many cases, they aren't.

下面的示例代码无法捕获Unix信号,因此-如您在Linux和OS X上所注意到的-它只会终止直接后代进程,而不会终止任何可能从其启动的后续进程.您将需要处理Unix信号来解决该问题. >

The example code below does not catch Unix signals, so -- as you'll notice on Linux and OS X -- it'll only terminate the direct descendant process, not any subsequent ones that might have been launched from it. You would need to handle Unix signals to fix that.

//main.cpp
#include <QApplication>
#include <QPushButton>
#include <QProcess>

class Launcher : public QObject
{
    Q_OBJECT
    int n;
    QProcess * process;
signals:
    void setDisabled(bool);
public slots:
    void launch() {
        QStringList args;
        args << QString::number(n);
        process = new QProcess(this);
        process->start(QCoreApplication::applicationFilePath(), args);
        connect(qApp, SIGNAL(aboutToQuit()), process, SLOT(terminate()));
        emit setDisabled(true);
    }
public:
    Launcher(int no) : n(no), process(0) {}
    ~Launcher() {
        if (process) {
            process->terminate();
            process->waitForFinished();
        }
    }
};

int main(int argc, char ** argv)
{
    QApplication a(argc, argv);
    int n = 0;
    if (argc > 1) n = QByteArray(argv[1]).toInt();
    Launcher launcher(n+1);
    QPushButton spawn(QString("Spawn #%1").arg(n));
    launcher.connect(&spawn, SIGNAL(clicked()), SLOT(launch()));
    spawn.connect(&launcher, SIGNAL(setDisabled(bool)), SLOT(setDisabled(bool)));
    spawn.show();
    return a.exec();
}

#include "main.moc"

这篇关于确保QProcess在其父QThread终止时终止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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