Results 1 to 5 of 5

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

Threaded View

Previous Post Previous Post   Next Post Next Post
  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

Similar Threads

  1. QProcess in a QThread
    By chombium in forum Qt Programming
    Replies: 2
    Last Post: 11th January 2006, 15: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.