Results 1 to 8 of 8

Thread: Time out for a QProcess?

  1. #1
    Join Date
    Nov 2009
    Posts
    59
    Thanks
    20
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Time out for a QProcess?

    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
    Qt Code:
    1. #include <QtCore/QCoreApplication>
    2. #include<QProcess>
    3. #include<stdio.h>
    4. class TIMEOUT{
    5. public:
    6. signals:
    7. void timeout();
    8. };
    9. void TIMEOUT::timeout(){
    10. //Help me code there. Thanks
    11. }
    12.  
    13. int main(int argc, char *argv[])
    14. {
    15. QCoreApplication a(argc, argv);
    16. QProcess proc;
    17. TIMEOUT timeout;
    18. QObject::connect(&proc, SIGNAL(timeout.timeout()),&a, SLOT(quit()));
    19. proc.execute("C:\\Tesst.exe");
    20. printf("Hello");
    21. proc.waitForFinished(1000);
    22. return a.exec();
    23. }
    To copy to clipboard, switch view to plain text mode 
    thanks

  2. #2
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Time out for a QProcess?

    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.


  3. #3
    Join Date
    Nov 2009
    Posts
    59
    Thanks
    20
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Time out for a QProcess?

    as you see above i have
    Qt Code:
    1. proc.waitForFinished(1000);
    To copy to clipboard, switch view to plain text mode 
    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
    Last edited by nthung; 4th October 2011 at 11:34.

  4. #4
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Time out for a QProcess?

    first of all, your connect() statement is incorrect. secondly your approach to the problem is incorrect.

  5. The following user says thank you to nish for this useful post:

    nthung (5th October 2011)

  6. #5
    Join Date
    Nov 2009
    Posts
    59
    Thanks
    20
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Time out for a QProcess?

    Quote Originally Posted by nish View Post
    first of all, your connect() statement is incorrect. secondly your approach to the problem is incorrect.
    Thanks
    Could you show my errors to me?

  7. #6
    Join Date
    Nov 2009
    Posts
    59
    Thanks
    20
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Time out for a QProcess?

    Quote Originally Posted by nthung View Post
    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

  8. #7
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Time out for a QProcess?

    Here is an incomplete list of things to consider (look for the comments):
    Qt Code:
    1. #include <QtCore/QCoreApplication>
    2. #include<QProcess>
    3. #include<stdio.h>
    4. class TIMEOUT{ // << needs to be derived from QObject to contain signals/slots
    5. // << needs the Q_OBJECT macro for the same reason
    6. public:
    7. signals:
    8. void timeout(); // << this should be a slot, not a signal
    9. };
    10. void TIMEOUT::timeout(){
    11. //Help me code there. Thanks
    12. // << You will need code to terminate the process
    13. }
    14.  
    15. int main(int argc, char *argv[])
    16. {
    17. QCoreApplication a(argc, argv);
    18. QProcess proc;
    19. TIMEOUT timeout;
    20. QObject::connect(&proc, SIGNAL(timeout.timeout()),&a, SLOT(quit())); // << QProcess has no signal called timeout.timeout()
    21. proc.execute("C:\\Tesst.exe"); // << this executes the program and waits for it to finish
    22. printf("Hello"); // << Hi!
    23. proc.waitForFinished(1000); // << pointless, the Tesst program has finished before it can get here
    24. return a.exec();
    25. }
    To copy to clipboard, switch view to plain text mode 

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

    The structure of your program will change quite a lot (or it won't compile)
    Last edited by ChrisW67; 5th October 2011 at 06:36.

  9. #8
    Join Date
    Nov 2009
    Posts
    59
    Thanks
    20
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Time out for a QProcess?

    thank you so much

Similar Threads

  1. Replies: 5
    Last Post: 19th November 2010, 02:25
  2. Replies: 0
    Last Post: 26th August 2010, 10:44
  3. Replies: 6
    Last Post: 18th August 2010, 12:52
  4. CPU Time & Memory Usage in QThread or QProcess
    By Davidaino in forum Qt Programming
    Replies: 0
    Last Post: 11th July 2008, 19:15
  5. check the time of last time keyboard is pressed
    By Aki Tomar in forum General Programming
    Replies: 2
    Last Post: 5th February 2008, 09:10

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.