PDA

View Full Version : how to stop thread while thread is running?



madawg
30th May 2018, 08:49
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:

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?

ChristianEhrlicher
30th May 2018, 17:02
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