How to stop thread while thread is still running?
-
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? -
@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();
.. -
@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();
.. -
@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.