TimeRun Cit
TimeRun Cit

Reputation: 177

How to check if a Process is Running or Not

I am starting a process using the below code

 QProcess* process = new QProcess();
 process->start(Path);

The start method will start a third party application.

If the process is already running, I should not call process->start(Path) again.

The process pointer is private member of the class.

Upvotes: 4

Views: 26455

Answers (2)

Neurotransmitter
Neurotransmitter

Reputation: 6797

To complement the @jdi's answer with a real-life code example:

QString executable = "C:/Program Files/tool.exe";
QProcess *process = new QProcess(this);
process->start(executable, QStringList());

// some code

if ( process->state() == QProcess::NotRunning ) {
    // do something
};

QProcess::ProcessState constants are:

Constant              Value Description
QProcess::NotRunning    0   The process is not running.
QProcess::Starting      1   The process is starting, but the program has not yet been invoked.
QProcess::Running       2   The process is running and is ready for reading and writing.

Documentation is here.

Upvotes: 3

jdi
jdi

Reputation: 92569

From the docs for QProcess ...

There are at least 3 ways to check if a QProcess instance is running.

QProcess.pid() : If its running, the pid will be > 0

QProcess.state() : Check it again the ProcessState enum to see if its QProcess::NotRunning

QProcess.atEnd() : Its not running if this is true

If any of these are not working as you would expect, then you will need to post a specific case of that example.

Upvotes: 12

Related Questions