Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 15th, 2012  |  syntax: None  |  size: 6.07 KB  |  hits: 14  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Terminate an ongoing QProcess that is running inside a QThread?
  2. DbManager::extCmd(){
  3.     ...
  4.     QMutexLocker locker(&extCmdProcessLock);
  5.     extCmdProcess = new QProcess(this);
  6.     QString argStr  += " --p1=1"
  7.                     +  " --p2=3";
  8.     extCmdProcess->start(cmd,argStr.split(QString(" ")));
  9.     bool startedSuccessfully = extCmdProcess->waitForStarted();
  10.     if (!startedSuccessfully) {
  11.        extCmdProcess->close();
  12.        extCmdProcess->kill();
  13.        extCmdProcess->waitForFinished();
  14.        delete extCmdProcess;
  15.        extCmdProcess = NULL;
  16.        return;
  17.     }
  18.     bool successfullyFinished = extCmdProcess->waitForFinished(-1);
  19.     if (!successfullyFinished) {
  20.        qDebug() << "finishing failed"; // Appendix C
  21.        extCmdProcess->close();
  22.        extCmdProcess->kill();
  23.        extCmdProcess->waitForFinished(-1);
  24.        delete extCmdProcess;
  25.        extCmdProcess = NULL;
  26.        return;
  27.    }
  28.    extCmdProcess->close();
  29.    delete extCmdProcess;
  30.    extCmdProcess = NULL;
  31. }
  32.  
  33. DbManager::~DbManager(){
  34.     qDebug() << "DB DbManager destructor called.";  
  35.     QMutexLocker locker(&extCmdProcessLock);
  36.     if (extCmdProcess!= NULL){
  37.        this->extCmdProcess->kill(); // added after Appendix A
  38.        this->extCmdProcess->waitForFinished();
  39.     }
  40. }
  41.        
  42. #0  0x00007f25e03a492a in QEventDispatcherUNIX::registerSocketNotifier () from /usr/local/Trolltech/Qt-4.7.4/lib/libQtCore.so.4
  43. #1  0x00007f25e0392d0b in QSocketNotifier::QSocketNotifier () from /usr/local/Trolltech/Qt-4.7.4/lib/libQtCore.so.4
  44. #2  0x00007f25e0350bf8 in ?? () from /usr/local/Trolltech/Qt-4.7.4/lib/libQtCore.so.4
  45. #3  0x00007f25e03513ef in ?? () from /usr/local/Trolltech/Qt-4.7.4/lib/libQtCore.so.4
  46. #4  0x00007f25e03115da in QProcess::start () from /usr/local/Trolltech/Qt-4.7.4/lib/libQtCore.so.4
  47. #5  0x0000000000428628 in DbManager::extCmd()
  48. #6  0x000000000042ca06 in DbManager::storePos ()
  49. #7  0x000000000044f51c in DeviceConnection::incomingData ()
  50. #8  0x00000000004600fb in DeviceConnection::qt_metacall ()
  51. #9  0x00007f25e0388782 in QObject::event () from /usr/local/Trolltech/Qt-4.7.4/lib/libQtCore.so.4
  52. #10 0x00007f25e0376e3f in QCoreApplicationPrivate::notify_helper () from /usr/local/Trolltech/Qt-4.7.4/lib/libQtCore.so.4
  53. #11 0x00007f25e0376e86 in QCoreApplication::notify () from /usr/local/Trolltech/Qt-4.7.4/lib/libQtCore.so.4
  54. #12 0x00007f25e0376ba4 in QCoreApplication::notifyInternal () from /usr/local/Trolltech/Qt-4.7.4/lib/libQtCore.so.4
  55. #13 0x00007f25e0377901 in QCoreApplicationPrivate::sendPostedEvents () from /usr/local/Trolltech/Qt-4.7.4/lib/libQtCore.so.4
  56. #14 0x00007f25e03a4500 in QEventDispatcherUNIX::processEvents () from /usr/local/Trolltech/Qt-4.7.4/lib/libQtCore.so.4
  57. #15 0x00007f25e0375e15 in QEventLoop::processEvents () from /usr/local/Trolltech/Qt-4.7.4/lib/libQtCore.so.4
  58. #16 0x00007f25e0376066 in QEventLoop::exec () from /usr/local/Trolltech/Qt-4.7.4/lib/libQtCore.so.4
  59. #17 0x00007f25e0277715 in QThread::exec () from /usr/local/Trolltech/Qt-4.7.4/lib/libQtCore.so.4
  60. #18 0x00007f25e027a596 in ?? () from /usr/local/Trolltech/Qt-4.7.4/lib/libQtCore.so.4
  61. #19 0x00007f25df9b43f7 in start_thread () from /lib/libpthread.so.0
  62. #20 0x00007f25def89b4d in clone () from /lib/libc.so.6
  63. #21 0x0000000000000000 in ?? ()
  64.        
  65. #0  0x00007fc94e9796b0 in QProcess::setProcessState () from /usr/local/Trolltech/Qt-4.7.4/lib/libQtCore.so.4
  66. #1  0x00007fc94e97998b in QProcess::waitForStarted () from /usr/local/Trolltech/Qt-4.7.4/lib/libQtCore.so.4
  67. #2  0x00007fc94e979a12 in QProcess::waitForFinished () from /usr/local/Trolltech/Qt-4.7.4/lib/libQtCore.so.4
  68. #3  0x0000000000425681 in DbManager::extCmd()
  69. #4  0x0000000000426fb6 in DbManager::storePos ()
  70. #5  0x000000000044d51c in DeviceConnection::incomingData ()
  71. #6  0x000000000045fb7b in DeviceConnection::qt_metacall ()
  72. #7  0x00007fc94e9f4782 in QObject::event () from /usr/local/Trolltech/Qt-4.7.4/lib/libQtCore.so.4
  73. #8  0x00007fc94e9e2e3f in QCoreApplicationPrivate::notify_helper () from /usr/local/Trolltech/Qt-4.7.4/lib/libQtCore.so.4
  74. #9  0x00007fc94e9e2e86 in QCoreApplication::notify () from /usr/local/Trolltech/Qt-4.7.4/lib/libQtCore.so.4
  75. #10 0x00007fc94e9e2ba4 in QCoreApplication::notifyInternal () from /usr/local/Trolltech/Qt-4.7.4/lib/libQtCore.so.4
  76. #11 0x00007fc94e9e3901 in QCoreApplicationPrivate::sendPostedEvents () from /usr/local/Trolltech/Qt-4.7.4/lib/libQtCore.so.4
  77. #12 0x00007fc94ea10500 in QEventDispatcherUNIX::processEvents () from /usr/local/Trolltech/Qt-4.7.4/lib/libQtCore.so.4
  78. #13 0x00007fc94e9e1e15 in QEventLoop::processEvents () from /usr/local/Trolltech/Qt-4.7.4/lib/libQtCore.so.4
  79. #14 0x00007fc94e9e2066 in QEventLoop::exec () from /usr/local/Trolltech/Qt-4.7.4/lib/libQtCore.so.4
  80. #15 0x00007fc94e8e3715 in QThread::exec () from /usr/local/Trolltech/Qt-4.7.4/lib/libQtCore.so.4
  81. #16 0x00007fc94e8e6596 in ?? () from /usr/local/Trolltech/Qt-4.7.4/lib/libQtCore.so.4
  82. #17 0x00007fc94e0203f7 in start_thread () from /lib/libpthread.so.0
  83. #18 0x00007fc94d5f5b4d in clone () from /lib/libc.so.6
  84. #19 0x0000000000000000 in ?? ()
  85.        
  86. class Query : public QObject
  87. {
  88.   Q_OBJECT
  89. public:
  90.   Query(QObject * parent = 0) : QObject(parent) {
  91.     connect(process, SIGNAL(error(QProcess::ProcessError)), SLOT(error()));
  92.     connect(process, SIGNAL(finished(int,QProcess::ExitStatus)), SLOT(finished(int,QProcess::ExitStatus)));
  93.   }
  94.   ~Query() { process.kill(); }
  95.   void start() {
  96.     QTimer::singleShot(0, this, SLOT(slot1()));
  97.   }
  98. protected slots:
  99.   void slot1() {
  100.     // do a database query
  101.     process.start(....);
  102.     next = &Query::slot2;
  103.   }
  104. protected:
  105.   // slot2 and slot3 don't have to be slots
  106.   void slot2() {
  107.     if (result == Error) {...}
  108.     else {...}
  109.     // another database query
  110.     process.start(...); // yet another process gets fired
  111.     next = &Query::slot3;
  112.   }
  113.   void slot3() {
  114.     if (result == Error) {...}
  115.     deleteLater();
  116.   }
  117.  
  118. protected slots:
  119.   void error() {
  120.     result = Error;
  121.     (this->*next)();
  122.   }
  123.   void finished(int code, QProcess::ExitStatus status) {
  124.     result = Finished;
  125.     exitCode = code;
  126.     exitStatus = status;
  127.     (this->*next)();
  128.   }
  129. private:
  130.   QProcess process;
  131.   enum { Error, Finished } result;
  132.   int exitCode;
  133.   QProcess::ExitStatus exitStatus;
  134.   void (Query::* next)();
  135. };