简体   繁体   中英

QProcess terminate process tree

I am currently working on a project with Qt5.4 and C++. In this project I start and stop Processes with the QProcess class.

I am now extending the project to start batch files. The Problem is that I want to terminate/kill the processes started with the batch file, using the QProcess. Calling terminate does not work (or maybe I am calling it wrong)

edit: QProcess is a Member(pointer) of a class called ProcessHolder. startProcess() and stopProcess() handle the process.

bool ProcessHolder::startProcess(const QString &path, 
                                 const QStringList  &args) {
    process_->start(path, args);
    qDebug() << process_->errorString();
    if(process_->waitForStarted(1000)) {
        state_ = ProcessState::running;
        return true;
    } else {
        state_ = ProcessState::fail;
        return false;
    }
}

bool ProcessHolder::stopProcess() {
    process_->terminate();
    state_ = ProcessState::notRunning;
    return true;
}

Please help me, Ben

So Qt does not provide an anwser for this kind of problem. Windows seems to don't have a proper implementation of a process tree.

You can use CreateToolhelp32Snapshot to view all processes and there parent-processes.

then you have to build a tree and erase it by hand. You can use QProcess::processID() to get the root process for that tree.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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