title: How to implement frequent start/stop of a thread (QThread)
body: <p>I need to start and stop a thread very frequently using push button..I am using Qt. Recently I learned to create a QObject of the worker and move it to the object of the QThread as the correct way of implementing threads in Qt. Following is my implementation...</p>
  
  <p><strong>Worker.h</strong></p>
  
  <pre><code>class worker : public QObject
  {
      Q_OBJECT
  public:
      explicit worker(QObject *parent = 0);
      void StopWork();
      void StartWork();
      bool IsWorkRunning();
  
  signal:
      void SignalToObj_mainThreadGUI();
  
  public slots:
      void do_Work();
  
  private:
      void Sleep();
  
      volatile bool running,stopped;
      QMutex mutex;
      QWaitCondition waitcondition;
  };
  </code></pre>
  
  <p><strong>Worker.cpp</strong></p>
  
  <pre><code> worker::worker(QObject *parent) :
      QObject(parent),stopped(false),running(false)
  {
  }
  
  void worker::do_Work()
  {
      running = true;
      while(!stopped)
      {
          emit SignalToObj_mainThreadGUI();
          Sleep();
      }
  }
  
  void worker::Sleep()
  {
      mutex.lock();
      waitcondition.wait(&amp;mutex,10);
      mutex.unlock();
  }
  
  void worker::StopWork()
  {
      mutex.lock();
      stopped = true;
      running = false;
      mutex.unlock();
  }
  
  void worker::StartWork()
  {
      mutex.lock();
      stopped = false;
      running = true;
      mutex.unlock();
  }
  
  bool worker::IsWorkRunning()
  {
      return running;
  }
  </code></pre>
  
  <p><strong>MainWindow.h</strong></p>
  
  <pre><code>namespace Ui {
  class MainWindow;
  }
  
  class MainWindow : public QMainWindow
  {
      Q_OBJECT
  
  public:
      explicit MainWindow(QWidget *parent = 0);
      ~MainWindow();
  
  public slots:
  
  private slots:
      void on_pushButton_push_to_start_clicked();
  
      void on_pushButton_push_to_stop_clicked();
  
  private:
  
      Ui::MainWindow *ui;
      worker *myWorker;
      QThread *WorkerThread;
  };
  </code></pre>
  
  <p><strong>MainWindow.cpp</strong></p>
  
  <pre><code>MainWindow::MainWindow(QWidget *parent) :
      QMainWindow(parent),
      ui(new Ui::MainWindow)
  {
      ui-&gt;setupUi(this);
  
      myWorker = new worker;
      WorkerThread = new QThread;
      myWorker.moveToThread(WorkerThread);
  
      QObject::connect(WorkerThread,SIGNAL(started()),myWorker,SLOT(do_Work()));
  }
  
  MainWindow::~MainWindow()
  {
      delete ui;
  }
  
  void MainWindow::on_pushButton_push_to_start_clicked()
  {
      if(!myWorker.IsWorkRunning())
         {
           myWorker-&gt;StartWork();
           WorkerThread-&gt;start();
         }
  }
  
  void MainWindow::on_pushButton_push_to_stop_clicked()
  {
    if(myWorker.IsWorkRunning())
       {
         myWorker-&gt;StopWork();
         WorkerThread-&gt;quit();
       }
  }
  </code></pre>
  
  <p>Initially the application works fine but after some operations of the push button to turn the working of the thread on and off the following error comes up...</p>
  
  <p><strong>QObject::killTimers(): timers cannot be stopped from another thread</strong></p>
  
  <p>I am quite puzzled at this error...do I need to implement the start/stop functions as signals and slots rather than member functions of the class?</p>