Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to stop thread while thread is still running?

How to stop thread while thread is still running?

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 6.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kien Bui
    wrote on last edited by
    #1

    I have a Class DeviceManager:

    DeviceManager(QObject *parent) :
        QObject(parent)
    {
        thread = new QThread();
        this->moveToThread(thread);
        connect(thread, SIGNAL(started()), this, SLOT(autoDetect()));
        thread->start();
    }
    
    void autoDetect()
    {
        ...
        emit addDevice();
        ... 
    }
    

    Class MainForm:

    [CODE]MainForm(QObject *parent) :
        QObject(parent)
    {
        this->manager = new DeviceManager();
        connect(manager, SIGNAL(addDevice()), this, SLOT(slot()));
    
    }
    
    void refresh()
    {
         this->manager->deleteLater();
         this->manager = new DeviceManager();
         connect(manager, SIGNAL(addDevice()), this, SLOT(slot()));
    }
    

    I have a problem is manager don't disconnect. When mainForm call refresh many time (eg: 3 times), then when slot() is executed 4 times.
    I think that the older manager's connect is disconnect or object can't deleted.
    I tried delete thread but I got message: QThread: Destroyed while thread is still running
    How to stop thread while thread is still running?

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Kien-Bui said in How to stop thread while thread is still running?:

      this->moveToThread(thread);

      This seems not right.
      Normally you would move a Worker object.
      https://mayaposch.wordpress.com/2011/11/01/how-to-really-truly-use-qthreads-the-full-explanation/

      to stop a thread, you can call
      quit() / exit() and as last resort terminate() ( bad, see docs warning)

      the normal way is
      qthread.quit(); // Cause the thread to cease.
      qthread.wait();
      ..

      K 1 Reply Last reply
      2
      • mrjjM mrjj

        @Kien-Bui said in How to stop thread while thread is still running?:

        this->moveToThread(thread);

        This seems not right.
        Normally you would move a Worker object.
        https://mayaposch.wordpress.com/2011/11/01/how-to-really-truly-use-qthreads-the-full-explanation/

        to stop a thread, you can call
        quit() / exit() and as last resort terminate() ( bad, see docs warning)

        the normal way is
        qthread.quit(); // Cause the thread to cease.
        qthread.wait();
        ..

        K Offline
        K Offline
        Kien Bui
        wrote on last edited by
        #3

        @mrjj
        if I use:

        qthread.quit(); // Cause the thread to cease.
        qthread.wait();
        

        I have a problem, the object in this thread has the connection with another object, and they don't disconnect immediately.

        jsulmJ 1 Reply Last reply
        0
        • K Kien Bui

          @mrjj
          if I use:

          qthread.quit(); // Cause the thread to cease.
          qthread.wait();
          

          I have a problem, the object in this thread has the connection with another object, and they don't disconnect immediately.

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Kien-Bui Then disconnect before calling quit()/wait()

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          3

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved