title: QThread - Call a method of another thread
body: <p>I'm trying to call a method of another thread (not the Main), using invokeMethod. Unfortunately that makes the application crash as soon as it tries to execute the invokeMethod!</p>
  
  <p>Am I mistaking something?</p>
  
  <p>// main.cpp</p>
  
  <pre><code>#include &lt;QtCore&gt;
  #include "entrypointclass.h"
  int main(int argc, char *argv[])
  {
      QCoreApplication a(argc, argv);
  
      qDebug() &lt;&lt; a.thread()-&gt;currentThreadId() &lt;&lt; " - Application started.";
  
      EntryPointClass entryPoint;
      entryPoint.runInNewThread();
      return a.exec();
  }
  </code></pre>
  
  <p>// Entrypoint.h</p>
  
  <pre><code>#ifndef ENTRYPOINTCLASS_H
  #define ENTRYPOINTCLASS_H
  
  #include "worker.h"
  #include &lt;QtCore&gt;
  
  class EntryPointClass : public QObject
  {
      Q_OBJECT
  public:
      EntryPointClass();
      ~EntryPointClass();
      void runInNewThread();
  public slots:
      void timeoutExpired();
  private:
      Worker* m_Worker;
      QThread* m_Thread;
  };
  
  #endif // ENTRYPOINTCLASS_H
  </code></pre>
  
  <p>// Entrypoint.cpp</p>
  
  <pre><code>#include &lt;QTCore&gt;
  #include "entrypointclass.h"
  #include "Worker.h"
  
  EntryPointClass::EntryPointClass()
  {
      qDebug() &lt;&lt; "EntryPointClass created";
  
  
  
  }
  
  EntryPointClass::~EntryPointClass()
  {
      qDebug() &lt;&lt; "EntryPointClass destroyed";
  }
  
  void EntryPointClass::runInNewThread()
  {
      QThread* m_Thread = new QThread;
      Worker* m_Worker = new Worker();
  
  
      connect(m_Thread, SIGNAL(started()), m_Worker, SLOT(doSomething()));
      connect(m_Worker, SIGNAL(finished()), m_Thread, SLOT(quit()));
      connect(m_Thread, SIGNAL(finished()), m_Thread, SLOT(deleteLater()));
      connect(m_Thread, SIGNAL(finished()), m_Worker, SLOT(deleteLater()));
  
      QTimer* timer = new QTimer;
      timer-&gt;setSingleShot(true);
  
      //bool bOK = connect(timer, SIGNAL(timeout()), m_Worker, SLOT(closeWorker()), Qt::BlockingQueuedConnection);
  
      connect(timer, SIGNAL(timeout()), this, SLOT(timeoutExpired()));
  
      m_Worker-&gt;moveToThread(m_Thread);
      m_Thread-&gt;start();
      timer-&gt;start(5000);
  
  }
  
  void EntryPointClass::timeoutExpired()
  {
      qDebug() &lt;&lt; "timeout expired";
      if (m_Worker != NULL)
      {
          QMetaObject::invokeMethod(m_Worker, "closeWorker", Qt::QueuedConnection);
      }
  }
  </code></pre>
  
  <p>// Worker.h</p>
  
  <pre><code>#ifndef WORKER_H
  #define WORKER_H
  
  #include &lt;QtCore&gt;
  
  class Worker : public QObject
  {
      Q_OBJECT
  public:
      Worker();
      ~Worker();
  
  public slots:
      void doSomething();
      void closeWorker();
  
  private:
      bool m_bAbort;
      QMutex m_mutex;
  
  
  signals:
      void finished();
  };
  
  #endif // WORKER_H
  </code></pre>
  
  <p>// Worker.cpp</p>
  
  <pre><code>#include "worker.h"
  #include &lt;unistd.h&gt;
  #include "QTcore"
  
  Worker::Worker()
      : m_mutex()
  {
      qDebug() &lt;&lt; this-&gt;thread()-&gt;currentThreadId() &lt;&lt; "Worker created";
      m_bAbort = false;
      //qDebug() &lt;&lt; QString("Thread %1 - Worker created").arg("");//this-&gt;thread()-&gt;currentThreadId());
  }
  
  Worker::~Worker()
  {
      qDebug() &lt;&lt; this-&gt;thread()-&gt;currentThreadId() &lt;&lt; "Worker destroyed";
  
  }
  
  void Worker::doSomething()
  {
      while(!m_bAbort)
      {
          sleep(2);
          qDebug() &lt;&lt; this-&gt;thread()-&gt;currentThreadId() &lt;&lt; "Do Something!";
      }
  
      emit finished();
  }
  
  void Worker::closeWorker()
  {
      qDebug() &lt;&lt; this-&gt;thread()-&gt;currentThreadId() &lt;&lt; "closeWorker triggered!";
      QMutexLocker mutexLocker(&amp;m_mutex);
      m_bAbort = true;
  }
  </code></pre>