PDA

View Full Version : unable to terminate QProcess in QThread, returns wrong status



sector
27th July 2006, 10:09
Hello,

I'm trying to launch process using QProcess in QThread, this work with no problem. However, if I check status of the running process, it's always 0 (QProcess::NotRunning).

I attach zipped example which ilustrates the problem, most interesting parts of code:

qmythread.cpp


#include <QProcess>
#include "qmythread.h"

QMyThread::QMyThread(QObject *parent)
: QThread(parent)
{
}

void QMyThread::setParameters(const QString& command)
{
cmd = command;
}

void QMyThread::run()
{
myProcess.execute(cmd);
exec();
}

void QMyThread::teminateProcess()
{
qDebug() << "Attempt to terminate..";
myProcess.terminate();
}

int QMyThread::stateOfProcess()
{
return myProcess.state();
}


myqtapp.cpp


#include <QtGui>

#include "myqtapp.h"
#include "qmythread.h"


myQtApp::myQtApp()
{
setupUi(this);
connect( pushButton_launch, SIGNAL( clicked() ), this, SLOT( launch() ) );
connect( pushButton, SIGNAL( clicked() ), this, SLOT( stop() ) );
}


void myQtApp::launch()
{
qDebug() << "Launching program..";

thread = new QMyThread(this);
thread->setParameters("regedit.exe");
thread->start();
}


void myQtApp::stop()
{
if ( thread->isRunning() )
{
qDebug() << "Thread is running..";
qDebug() << "State of process: " << thread->stateOfProcess();
// state of process always returns 0 (QProcess::NotRunning), even if it's running
thread->teminateProcess();
// attempt to terminate process running in thread is unsuccessful
}
else
{
qDebug() << "Error, thread not running";
}
}


This might be a bug in QT but I don't know if I'm overlooking something or if there is something wrong with my code.

I use Qt 4.2.0-tp1 opensource, mingw, win2000. Thanks for your replies.

jacek
27th July 2006, 10:17
Qt docs say:
int QProcess::execute ( const QString & program, const QStringList & arguments ) [static]
Starts the program program with the arguments arguments in a new process, waits for it to finish, and then returns the exit code of the process. Any data the new process writes to the console is forwarded to the calling process.
The environment and working directory are inherited by the calling process.
On Windows, arguments that contain spaces are wrapped in quotes.
int QProcess::execute ( const QString & program ) [static]
This is an overloaded member function, provided for convenience.
Starts the program program in a new process. program is a single string of text containing both the program name and its arguments. The arguments are separated by one or more spaces.
These are static methods, so you don't need QProcess instance to invoke them and even if you do use it, they won't influence it.

jpn
27th July 2006, 10:28
Are you sure you even need a thread..? :) QProcess launches an external process so it won't block anything.

Edit: Oh, and by the way, you are calling myprocess object's methods from 2 separate threads.

QMyThread::teminateProcess() and QMyThread::stateOfProcess() are executed in the main thread. QMyThread::run() is executed in the separate thread.

sector
27th July 2006, 10:47
Ah,.. you are right but if I make something like this



void myQtApp::launch()
{
QProcess proc;

proc.start("regedit.exe");

}


I get QProcess: Destroyed while process is still running.

If I define proc as member of myQtApp it works, nah.. :] This tempted me to use execute() and thread.. It works fine, thanks for your time.



Edit: Oh, and by the way, you are calling myprocess object's methods from 2 separate threads.

QMyThread::teminateProcess() and QMyThread::stateOfProcess() are executed in the main thread. QMyThread::run() is executed in the separate thread.


Is there something wrong with that ?

jacek
27th July 2006, 11:13
I get QProcess: Destroyed while process is still running.
Because proc gets destroyed as soon as launch() returns. You can create it on the heap (i.e. using new operator) to avoid this.


Is there something wrong with that ?
QProcess, just as every class derived from QObject, isn't thread-safe.