- Terminate an ongoing QProcess that is running inside a QThread?
- DbManager::extCmd(){
- ...
- QMutexLocker locker(&extCmdProcessLock);
- extCmdProcess = new QProcess(this);
- QString argStr += " --p1=1"
- + " --p2=3";
- extCmdProcess->start(cmd,argStr.split(QString(" ")));
- bool startedSuccessfully = extCmdProcess->waitForStarted();
- if (!startedSuccessfully) {
- extCmdProcess->close();
- extCmdProcess->kill();
- extCmdProcess->waitForFinished();
- delete extCmdProcess;
- extCmdProcess = NULL;
- return;
- }
- bool successfullyFinished = extCmdProcess->waitForFinished(-1);
- if (!successfullyFinished) {
- qDebug() << "finishing failed"; // Appendix C
- extCmdProcess->close();
- extCmdProcess->kill();
- extCmdProcess->waitForFinished(-1);
- delete extCmdProcess;
- extCmdProcess = NULL;
- return;
- }
- extCmdProcess->close();
- delete extCmdProcess;
- extCmdProcess = NULL;
- }
- DbManager::~DbManager(){
- qDebug() << "DB DbManager destructor called.";
- QMutexLocker locker(&extCmdProcessLock);
- if (extCmdProcess!= NULL){
- this->extCmdProcess->kill(); // added after Appendix A
- this->extCmdProcess->waitForFinished();
- }
- }
- #0 0x00007f25e03a492a in QEventDispatcherUNIX::registerSocketNotifier () from /usr/local/Trolltech/Qt-4.7.4/lib/libQtCore.so.4
- #1 0x00007f25e0392d0b in QSocketNotifier::QSocketNotifier () from /usr/local/Trolltech/Qt-4.7.4/lib/libQtCore.so.4
- #2 0x00007f25e0350bf8 in ?? () from /usr/local/Trolltech/Qt-4.7.4/lib/libQtCore.so.4
- #3 0x00007f25e03513ef in ?? () from /usr/local/Trolltech/Qt-4.7.4/lib/libQtCore.so.4
- #4 0x00007f25e03115da in QProcess::start () from /usr/local/Trolltech/Qt-4.7.4/lib/libQtCore.so.4
- #5 0x0000000000428628 in DbManager::extCmd()
- #6 0x000000000042ca06 in DbManager::storePos ()
- #7 0x000000000044f51c in DeviceConnection::incomingData ()
- #8 0x00000000004600fb in DeviceConnection::qt_metacall ()
- #9 0x00007f25e0388782 in QObject::event () from /usr/local/Trolltech/Qt-4.7.4/lib/libQtCore.so.4
- #10 0x00007f25e0376e3f in QCoreApplicationPrivate::notify_helper () from /usr/local/Trolltech/Qt-4.7.4/lib/libQtCore.so.4
- #11 0x00007f25e0376e86 in QCoreApplication::notify () from /usr/local/Trolltech/Qt-4.7.4/lib/libQtCore.so.4
- #12 0x00007f25e0376ba4 in QCoreApplication::notifyInternal () from /usr/local/Trolltech/Qt-4.7.4/lib/libQtCore.so.4
- #13 0x00007f25e0377901 in QCoreApplicationPrivate::sendPostedEvents () from /usr/local/Trolltech/Qt-4.7.4/lib/libQtCore.so.4
- #14 0x00007f25e03a4500 in QEventDispatcherUNIX::processEvents () from /usr/local/Trolltech/Qt-4.7.4/lib/libQtCore.so.4
- #15 0x00007f25e0375e15 in QEventLoop::processEvents () from /usr/local/Trolltech/Qt-4.7.4/lib/libQtCore.so.4
- #16 0x00007f25e0376066 in QEventLoop::exec () from /usr/local/Trolltech/Qt-4.7.4/lib/libQtCore.so.4
- #17 0x00007f25e0277715 in QThread::exec () from /usr/local/Trolltech/Qt-4.7.4/lib/libQtCore.so.4
- #18 0x00007f25e027a596 in ?? () from /usr/local/Trolltech/Qt-4.7.4/lib/libQtCore.so.4
- #19 0x00007f25df9b43f7 in start_thread () from /lib/libpthread.so.0
- #20 0x00007f25def89b4d in clone () from /lib/libc.so.6
- #21 0x0000000000000000 in ?? ()
- #0 0x00007fc94e9796b0 in QProcess::setProcessState () from /usr/local/Trolltech/Qt-4.7.4/lib/libQtCore.so.4
- #1 0x00007fc94e97998b in QProcess::waitForStarted () from /usr/local/Trolltech/Qt-4.7.4/lib/libQtCore.so.4
- #2 0x00007fc94e979a12 in QProcess::waitForFinished () from /usr/local/Trolltech/Qt-4.7.4/lib/libQtCore.so.4
- #3 0x0000000000425681 in DbManager::extCmd()
- #4 0x0000000000426fb6 in DbManager::storePos ()
- #5 0x000000000044d51c in DeviceConnection::incomingData ()
- #6 0x000000000045fb7b in DeviceConnection::qt_metacall ()
- #7 0x00007fc94e9f4782 in QObject::event () from /usr/local/Trolltech/Qt-4.7.4/lib/libQtCore.so.4
- #8 0x00007fc94e9e2e3f in QCoreApplicationPrivate::notify_helper () from /usr/local/Trolltech/Qt-4.7.4/lib/libQtCore.so.4
- #9 0x00007fc94e9e2e86 in QCoreApplication::notify () from /usr/local/Trolltech/Qt-4.7.4/lib/libQtCore.so.4
- #10 0x00007fc94e9e2ba4 in QCoreApplication::notifyInternal () from /usr/local/Trolltech/Qt-4.7.4/lib/libQtCore.so.4
- #11 0x00007fc94e9e3901 in QCoreApplicationPrivate::sendPostedEvents () from /usr/local/Trolltech/Qt-4.7.4/lib/libQtCore.so.4
- #12 0x00007fc94ea10500 in QEventDispatcherUNIX::processEvents () from /usr/local/Trolltech/Qt-4.7.4/lib/libQtCore.so.4
- #13 0x00007fc94e9e1e15 in QEventLoop::processEvents () from /usr/local/Trolltech/Qt-4.7.4/lib/libQtCore.so.4
- #14 0x00007fc94e9e2066 in QEventLoop::exec () from /usr/local/Trolltech/Qt-4.7.4/lib/libQtCore.so.4
- #15 0x00007fc94e8e3715 in QThread::exec () from /usr/local/Trolltech/Qt-4.7.4/lib/libQtCore.so.4
- #16 0x00007fc94e8e6596 in ?? () from /usr/local/Trolltech/Qt-4.7.4/lib/libQtCore.so.4
- #17 0x00007fc94e0203f7 in start_thread () from /lib/libpthread.so.0
- #18 0x00007fc94d5f5b4d in clone () from /lib/libc.so.6
- #19 0x0000000000000000 in ?? ()
- class Query : public QObject
- {
- Q_OBJECT
- public:
- Query(QObject * parent = 0) : QObject(parent) {
- connect(process, SIGNAL(error(QProcess::ProcessError)), SLOT(error()));
- connect(process, SIGNAL(finished(int,QProcess::ExitStatus)), SLOT(finished(int,QProcess::ExitStatus)));
- }
- ~Query() { process.kill(); }
- void start() {
- QTimer::singleShot(0, this, SLOT(slot1()));
- }
- protected slots:
- void slot1() {
- // do a database query
- process.start(....);
- next = &Query::slot2;
- }
- protected:
- // slot2 and slot3 don't have to be slots
- void slot2() {
- if (result == Error) {...}
- else {...}
- // another database query
- process.start(...); // yet another process gets fired
- next = &Query::slot3;
- }
- void slot3() {
- if (result == Error) {...}
- deleteLater();
- }
- protected slots:
- void error() {
- result = Error;
- (this->*next)();
- }
- void finished(int code, QProcess::ExitStatus status) {
- result = Finished;
- exitCode = code;
- exitStatus = status;
- (this->*next)();
- }
- private:
- QProcess process;
- enum { Error, Finished } result;
- int exitCode;
- QProcess::ExitStatus exitStatus;
- void (Query::* next)();
- };