Results 1 to 2 of 2

Thread: how to stop thread while thread is running?

  1. #1
    Join Date
    May 2018
    Posts
    19
    Thanks
    1
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default how to stop thread while thread is running?

    I have a Class DeviceManager:

    Qt Code:
    1. DeviceManager(QObject *parent) :
    2. QObject(parent)
    3. {
    4. thread = new QThread();
    5. this->moveToThread(thread);
    6. connect(thread, SIGNAL(started()), this, SLOT(autoDetect()));
    7. thread->start();
    8. }
    9.  
    10. void autoDetect()
    11. {
    12. ...
    13. emit addDevice();
    14. ...
    15. }
    To copy to clipboard, switch view to plain text mode 

    Class MainForm:
    Qt Code:
    1. MainForm(QObject *parent) :
    2. QObject(parent)
    3. {
    4. this->manager = new DeviceManager();
    5. connect(manager, SIGNAL(addDevice()), this, SLOT(slot()));
    6.  
    7. }
    8.  
    9. void refresh()
    10. {
    11. this->manager->deleteLater();
    12. this->manager = new DeviceManager();
    13. connect(manager, SIGNAL(addDevice()), this, SLOT(slot()));
    14. }
    To copy to clipboard, switch view to plain text mode 



    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?
    Last edited by madawg; 30th May 2018 at 08:53.

  2. #2
    Join Date
    Jan 2006
    Location
    Bremen, Germany
    Posts
    554
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to stop thread while thread is running?

    For disconnect: QObject has also a function for disconnecting signals & slots
    For thread: The thread has to leave the run() method. If you did not reimplement them, calling QThread::exit() should suffice. Then you must wait until the thread has finished - see QThread documentation

Similar Threads

  1. Replies: 0
    Last Post: 7th December 2017, 18:33
  2. Replies: 1
    Last Post: 6th December 2015, 16:12
  3. Replies: 4
    Last Post: 17th October 2013, 11:12
  4. Replies: 4
    Last Post: 24th September 2012, 07:28
  5. Stop the thread
    By santosh.kumar in forum Qt Programming
    Replies: 1
    Last Post: 14th May 2007, 10:29

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.