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 9203325
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:26:54+00:00 2026-06-17T23:26:54+00:00

I want to stop a looping thread when a signal was emitted so here

  • 0

I want to stop a looping thread when a signal was emitted so here is my code

void  MyThread::stopWatchingThread()
{
    qDebug()<<"MyThread::stopWatchingThread()";    
    Keep_running=false;
    qDebug()<<"MyThread::stopWatchingThread Keep_running"<<Keep_running;
    ...
}

void MyThread::run()
{
  qDebug()<<"MyThread::run()";
  qDebug()<<"MyThread::run Keep_running"<<Keep_running;  
  while(Keep_running)
    {
     ...
     }
  qDebug()<<"MyThread::run Keep_running"<<Keep_running;
  Keep_running=false;
  qDebug()<<"MyThread::run Keep_running"<<Keep_running;
}

void Watcher::Init()
{
    WatchingThread=new MyThread(this->L_RootToWatch);
    connect(this,SIGNAL(stopmonotiring()),WatchingThread, SLOT(stopWatchingThread()));
...
}
void Watcher::StartWatching()
{
    WatchingThread->start();
}

void Watcher::StopWatching()
{
    emit stopmonotiring();        
}

So every thing goes all right but my problem is that Keep_running never get false value in MyThread::run() after emitting stopWatchingThread and so while loop for ever.
What did I miss ?
any help will be appreciated.

  • 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-17T23:26:56+00:00Added an answer on June 17, 2026 at 11:26 pm

    Don’t create threaded classes explicitly in Qt. Instead, create a worker object, move that object to a QThread, then call start() on the QThread. Here’s a quick example:

    class Worker : public QObject
    {
      Q_OBJECT
    public:
      Worker( QObject * parent = 0 )
        : QObject( parent )
      {}
    
    public slots:
      void doWork( ... )
      { 
        // do work here
      }
    
      void stopMonitoring()
      { 
        emit finished();
      }
    
    signals:
      void finished();
    };
    
    int main()
    {
      Worker * w = new Worker();
      QThread * thread = new QThread();
      QObject::connect( w, SIGNAL(finished()), thread, SLOT(quit())
      QObject::connect( w, SIGNAL(finished()), w, SLOT(deleteLater())
      QObject::connect( thread, SIGNAL(finished()), thread, SLOT(deleteLater())
      w->moveToThread( thread );
      thread->start();
    
      // some other object emits a signal connected to the 'doWork()' slot.
    }
    

    I omitted some of the standard QApplication boiler-plate, but you have that already if you’re using Qt. This should get you started.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to make my looping stop when it's on the condition Here is
I want to run light music (non-stop) when user is running my application actively.
Here is a skeleton of my thread class: class MyThread { public: virutal ~MyThread();
I want to stop the end users from being able to resize a GUI
I want to stop click on any links until the page is loaded. Can
I want to stop outgoing call and open the application. The app is able
Is there any way to stop polling server in SignalR? I want to stop
For security purposes, I want to stop the users from being able to view
for example, if I want to stop lan connection 1 in my control pannel,
picturebox1.image = image.fromfile(c:/anim.gif) after i import the animation, i want to Stop the animation

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.