Qt start/kill external process

Reference 1

start external thread

There are:

  • QProcess ::execute() is opened by blocking the main process (static member);
  • QProcess ::start() opens as a child process of the main process (parent-child);
  • QProcess ::startDetached() opens in isolation;

(1) QProcess::execute() method

	QProcess::execute("C:\\Environment\\influxDB_1_5_2\\influxd.exe")

(2) QProcess::start() method

	QProcess  proc;
	proc.start("C:\\Environment\\influxDB_1_5_2\\influxd.exe");

(3) QProcess ::startDetached() method (recommended!)

    if (QProcess::startDetached("C:\\Environment\\influxDB_1_5_2\\influxd.exe"))
        qDebug()  <<"Running...";
    else
        qDebug()  <<"Failed";

Determine if the process is running

bool IsProcessExist(const QString &processName)// 返回 true/false
{
    
    
    QProcess proc;
    proc.start("tasklist");
    proc.waitForFinished();// 等待 tasklist 启动

    QByteArray result = proc.readAllStandardOutput();
    QString str = result;
    if(str.contains(processName))
    {
    
    
        qDebug() << processName <<"is Running";
        return true;
    }
    else
    {
    
    
        qDebug() << "Can't find " << processName;
        return false;
    }
}
bool IsProcessExist(const QString &processName)// 返回 true/false
{
    
    
    QString strInfo = QString::number(QProcess::execute("tasklist", QStringList()<<"-fi"<<"imagename eq influxd.exe"));
    if(strInfo .contains(processName))
    {
    
    
        qDebug() << processName <<"is Running";
        return true;
    }
    else
    {
    
    
        qDebug() << "Can't find " << processName;
        return false;
    }
}
void getProcessInfo() // 返回信息(需要对返回信息进行判断)
{
    
    
	QProcess::execute("tasklist", QStringList()<<"-fi"<<"imagename eq prog.exe");
}

terminate external process

	// 通过进程ID结束进程(该进程由proc对象打开前提是通过 proc)
    QProcess::startDetached("taskkill -t  -f /pid " + QString::number(proc.processId()));
	// 通过进程名字结束进程
    QProcess::startDetached("taskkill -t  -f /IM " + QString("influxd.exe"));

Guess you like

Origin blog.csdn.net/CXYYL/article/details/121540577