Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9227781
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:09:50+00:00 2026-06-18T05:09:50+00:00

I need to start and stop a thread very frequently using push button..I am

  • 0

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…

Worker.h

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;
};

Worker.cpp

 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(&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;
}

MainWindow.h

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;
};

MainWindow.cpp

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->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->StartWork();
         WorkerThread->start();
       }
}

void MainWindow::on_pushButton_push_to_stop_clicked()
{
  if(myWorker.IsWorkRunning())
     {
       myWorker->StopWork();
       WorkerThread->quit();
     }
}

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…

QObject::killTimers(): timers cannot be stopped from another thread

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T05:09:51+00:00Added an answer on June 18, 2026 at 5:09 am

    Don’t start and stop WorkerThread. Just leave it running. Also, move your StartWork() and StopWork() methods to the public slots section. You really don’t need the mutex at all.

    Worker.h

    class worker : public QObject
    {
      Q_OBJECT
    public:
      explicit worker(QObject *parent = 0);
    
    signal:
      void SignalToObj_mainThreadGUI();
      void running();
      void stopped();
    
    public slots:
      void StopWork();
      void StartWork();
    
    private slots:
      void do_Work();
    
    private:
      volatile bool running,stopped;
    };
    

    Worker.cpp

    worker::worker(QObject *parent) :
      QObject(parent), stopped(false), running(false)
    {}
    
    void worker::do_Work()
    {
      emit SignalToObj_mainThreadGUI();
    
      if ( !running || stopped ) return;
    
      // do important work here
    
      // allow the thread's event loop to process other events before doing more "work"
      // for instance, your start/stop signals from the MainWindow
      QMetaObject::invokeMethod( this, "do_Work", Qt::QueuedConnection );
    }
    
    void worker::StopWork()
    {
      stopped = true;
      running = false;
      emit stopped();
    }
    
    void worker::StartWork()
    {
      stopped = false;
      running = true;
      emit running();
      do_Work();
    }
    

    MainWindow.h

    namespace Ui {
      class MainWindow;
    }
    
    class MainWindow : public QMainWindow
    {
      Q_OBJECT
    public:
      explicit MainWindow(QWidget *parent = 0);
      ~MainWindow();
    
    signals:
      void startWork();
      void stopWork();
    
    private slots:
      void on_pushButton_push_to_start_clicked();
      void on_pushButton_push_to_stop_clicked();
    
    private:
      Ui::MainWindow *ui;
      worker *myWorker;
      QThread *WorkerThread;
    

    };

    MainWindow.cpp

    MainWindow::MainWindow(QWidget *parent) :
      QMainWindow(parent),
      ui(new Ui::MainWindow)
    {
      ui->setupUi(this);
    
      myWorker = new worker;
      WorkerThread = new QThread;
      myWorker.moveToThread(WorkerThread);
    
      connect( this, SIGNAL(startWork()), myWorker, SLOT(StartWork()) );
      connect( this, SIGNAL(stopWork()), myWorker, SLOT(StopWork()) );
    }
    
    void MainWindow::on_pushButton_push_to_start_clicked()
    {
      emit startWork();
    }
    
    void MainWindow::on_pushButton_push_to_stop_clicked()
    {
      emit stopWork();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a way to stop a worker thread that does not contain a
I need to be able to start/stop a per-session GUI agent from a root
I need to start a Visual Basic script file by using WMI in a
In my servlet I start a background thread onContextInitialized() and consequently I need to
In the main thread I need to do the following: Create a second thread
I need a timer class which should provide functionality to start/stop/enable/disable and should be
I need to start a loop from an event and then stop it from
I'm start to use GCD, and I need to know when a certain thread
I need a solution to properly stop the thread in Java. I have IndexProcessor
I need start off with code because I am not sure what terminology to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.