简体   繁体   English

QProcess 终止进程树

[英]QProcess terminate process tree

I am currently working on a project with Qt5.4 and C++.我目前正在使用 Qt5.4 和 C++ 进行一个项目。 In this project I start and stop Processes with the QProcess class.在这个项目中,我使用 QProcess 类启动和停止进程。

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.问题是我想使用 QProcess 终止/终止以批处理文件启动的进程。 Calling terminate does not work (or maybe I am calling it wrong)调用 terminate 不起作用(或者我可能错了)

edit: QProcess is a Member(pointer) of a class called ProcessHolder.编辑:QProcess 是一个名为 ProcessHolder 的类的成员(指针)。 startProcess() and stopProcess() handle the process. startProcess() 和 stopProcess() 处理进程。

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.所以 Qt 没有为这种问题提供 anwser。 Windows seems to don't have a proper implementation of a process tree. Windows 似乎没有正确实现进程树。

You can use CreateToolhelp32Snapshot to view all processes and there parent-processes.您可以使用CreateToolhelp32Snapshot查看所有进程和父进程。

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.您可以使用 QProcess::processID() 获取该树的根进程。

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

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