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

查看:39
本文介绍了确保 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.

这种误解像一种疾病一样蔓延,线程是解决每个人问题的方法.我的观察是,在带有 Qt 标签的 10 个帖子中,有 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天全站免登陆