Results 1 to 5 of 5

Thread: unable to terminate QProcess in QThread, returns wrong status

  1. #1
    Join Date
    Jul 2006
    Location
    Slovakia
    Posts
    17
    Thanks
    12
    Thanked 6 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default unable to terminate QProcess in QThread, returns wrong status

    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
    Qt Code:
    1. #include <QProcess>
    2. #include "qmythread.h"
    3.  
    4. QMyThread::QMyThread(QObject *parent)
    5. : QThread(parent)
    6. {
    7. }
    8.  
    9. void QMyThread::setParameters(const QString& command)
    10. {
    11. cmd = command;
    12. }
    13.  
    14. void QMyThread::run()
    15. {
    16. myProcess.execute(cmd);
    17. exec();
    18. }
    19.  
    20. void QMyThread::teminateProcess()
    21. {
    22. qDebug() << "Attempt to terminate..";
    23. myProcess.terminate();
    24. }
    25.  
    26. int QMyThread::stateOfProcess()
    27. {
    28. return myProcess.state();
    29. }
    To copy to clipboard, switch view to plain text mode 

    myqtapp.cpp
    Qt Code:
    1. #include <QtGui>
    2.  
    3. #include "myqtapp.h"
    4. #include "qmythread.h"
    5.  
    6.  
    7. myQtApp::myQtApp()
    8. {
    9. setupUi(this);
    10. connect( pushButton_launch, SIGNAL( clicked() ), this, SLOT( launch() ) );
    11. connect( pushButton, SIGNAL( clicked() ), this, SLOT( stop() ) );
    12. }
    13.  
    14.  
    15. void myQtApp::launch()
    16. {
    17. qDebug() << "Launching program..";
    18.  
    19. thread = new QMyThread(this);
    20. thread->setParameters("regedit.exe");
    21. thread->start();
    22. }
    23.  
    24.  
    25. void myQtApp::stop()
    26. {
    27. if ( thread->isRunning() )
    28. {
    29. qDebug() << "Thread is running..";
    30. qDebug() << "State of process: " << thread->stateOfProcess();
    31. // state of process always returns 0 (QProcess::NotRunning), even if it's running
    32. thread->teminateProcess();
    33. // attempt to terminate process running in thread is unsuccessful
    34. }
    35. else
    36. {
    37. qDebug() << "Error, thread not running";
    38. }
    39. }
    To copy to clipboard, switch view to plain text mode 

    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.
    Attached Files Attached Files

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: unable to terminate QProcess in QThread, returns wrong status

    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.

  3. The following user says thank you to jacek for this useful post:

    sector (27th July 2006)

  4. #3
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: unable to terminate QProcess in QThread, returns wrong status

    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.
    Last edited by jpn; 27th July 2006 at 11:32.
    J-P Nurmi

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

    sector (27th July 2006)

  6. #4
    Join Date
    Jul 2006
    Location
    Slovakia
    Posts
    17
    Thanks
    12
    Thanked 6 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: unable to terminate QProcess in QThread, returns wrong status

    Ah,.. you are right but if I make something like this

    Qt Code:
    1. void myQtApp::launch()
    2. {
    3. QProcess proc;
    4.  
    5. proc.start("regedit.exe");
    6.  
    7. }
    To copy to clipboard, switch view to plain text mode 

    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 ?

  7. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: unable to terminate QProcess in QThread, returns wrong status

    Quote Originally Posted by sector
    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.

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

  8. The following user says thank you to jacek for this useful post:

    sector (27th July 2006)

Similar Threads

  1. QProcess in a QThread
    By chombium in forum Qt Programming
    Replies: 2
    Last Post: 11th January 2006, 16:52

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.