PDA

View Full Version : Time out for a QProcess?



nthung
4th October 2011, 10:57
hi all.
I have a program(called Mother_Program) and this program calls other program( called Tesst.exe), and for 3 seconds the Tesst.exe must close, if Tesst.exe takes bigger than 3 seconds, Mother_Programm have raise time out. But i don't how to code the timeout function?
thanks
This is my code

#include <QtCore/QCoreApplication>
#include<QProcess>
#include<stdio.h>
class TIMEOUT{
public:
signals:
void timeout();
};
void TIMEOUT::timeout(){
//Help me code there. Thanks
}

int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QProcess proc;
TIMEOUT timeout;
QObject::connect(&proc, SIGNAL(timeout.timeout()),&a, SLOT(quit()));
proc.execute("C:\\Tesst.exe");
printf("Hello");
proc.waitForFinished(1000);
return a.exec();
}

thanks

nish
4th October 2011, 11:20
use QProcess::waitForFinished with a 3 sec timeout and check the condition of QProcess weather it has finished or not. then you can emit the appropriate signal.

nthung
4th October 2011, 11:27
as you see above i have
proc.waitForFinished(1000);
and how to know that it has finihed or not while it still runnign in another process
but seem that it does not work well

nish
4th October 2011, 11:34
first of all, your connect() statement is incorrect. secondly your approach to the problem is incorrect.

nthung
5th October 2011, 02:36
first of all, your connect() statement is incorrect. secondly your approach to the problem is incorrect.
Thanks
Could you show my errors to me?

nthung
5th October 2011, 04:56
Thanks
Could you show my errors to me?
Thanks
I think that when Tesst.exe take more than 1 second, althought that Tesst.exe still running, but Mother_Programwill pass proc.waitForFinished(1000);
Do you think that?
Thanks

ChrisW67
5th October 2011, 06:07
Here is an incomplete list of things to consider (look for the comments):


#include <QtCore/QCoreApplication>
#include<QProcess>
#include<stdio.h>
class TIMEOUT{ // << needs to be derived from QObject to contain signals/slots
// << needs the Q_OBJECT macro for the same reason
public:
signals:
void timeout(); // << this should be a slot, not a signal
};
void TIMEOUT::timeout(){
//Help me code there. Thanks
// << You will need code to terminate the process
}

int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QProcess proc;
TIMEOUT timeout;
QObject::connect(&proc, SIGNAL(timeout.timeout()),&a, SLOT(quit())); // << QProcess has no signal called timeout.timeout()
proc.execute("C:\\Tesst.exe"); // << this executes the program and waits for it to finish
printf("Hello"); // << Hi!
proc.waitForFinished(1000); // << pointless, the Tesst program has finished before it can get here
return a.exec();
}


To start and time out and terminate a sub process you will have to read:

Signal & Slots
QProcess::start(), QProcess::kill(), and all the signals

The structure of your program will change quite a lot (or it won't compile)

nthung
5th October 2011, 07:46
thank you so much