4

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.

3
  • have you looked at the docs? There are at least 3 different ways to know if the QProcess is running. Commented May 2, 2012 at 22:04
  • @jdi - I tried QProcess functions, but it always teating as Not Running. Commented May 2, 2012 at 22:08
  • Be careful with the syntax above. Either make sure it's destructing by manually calling it, or do QProcess* process = new QProcess(this); instead if your class inherits from QObject. Commented Apr 20, 2016 at 4:34

2 Answers 2

12

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks jdi, I think I implemented wrongly, using the process pointer directly i checked the NotRunning enum. I will try again properly.
@TimeRunCit: I'm not a C++ guy but just curious... If process is a private member, and you do QProcess* process = new QProcess();, is that not shadowing it with a local scope version each time?
You are correct, @jdi -- this has been vexing when dealing with multiple processes. When checking a list a pointers to a new process against any of those checks, you'll get false-positives.
3

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.