Exit SLOT when destroying QThread
-
Hello,
I have a class "Client" witch will create a QThread and an object of type Writer2class Client : public QObject { Q_OBJECT QThread workerThread; Client(){ Thread workerThread; Writer2 *writer2 = new Writer2(); writer2->moveToThread(&workerThread); } }
//Writer2.h has a SLOT 'testSlot()'
class Writer2 : public QObject { Q_OBJECT public slots : void testSlot(){ bool result = readAck() // a test if(result ){ emit ack_emerStop(true); qDebug()<<"ack OK..."; return; // if readAck() returns true its ok } else{ // if readAck() returns false, I wait 500ms and recall this slot (LOOP) QThread::msleep(500); qDebug()<<"reading ack again ..."; testSlot(); // recall } } }
My probleme is, if my testSlot() is looping, i can't quit my app properly... because QThread will be deleted but SLOT is still running in a thread.
So how to interrupt this SLOT execution in case i quit the application ?in client.cpp i have tryed lot of things unsuccessfully..
// QObject::connect(QCoreApplication::instance(),SIGNAL(aboutToQuit()),&workerThread,SLOT(deleteLater()));
// QObject::connect(this,SIGNAL(destroyed(QObject*)),&workerThread,SLOT(quit())); // terminate
//QObject::connect(&workerThread,SIGNAL(finished()),writer2,SLOT(deleteLater()))Thank you in advance,
LA -
Hi,
Do you mean you want to use an infinite loop in testSlot ?
By the way, you have something wrong in your Client constructor. You are creating a local
workerThread
object which is going to be destroyed by the end of the constructor which is likely not what you want. -
@SGaist Hi,
thank you for answeringYes, if 'readAck()' return 'false', I want 'testSlot()' to loop until 'readAck()' returns 'true' or user quit application ..
For the Client constructor part, its ok. I want to destroy everything when client is destructed.
-
Well, no it's not really. You are moving your object to another thread that is destroyed at the end of the constructor which doesn't really make sense.
As for your loop, why not use a QTimer to do the check periodically rather than that recursive loop ?
-
@SGaist thx.
I thought about using QTimer, i will try it.@SGaist said in Exit SLOT when destroying QThread:
Well, no it's not really. You are moving your object to another thread that is destroyed at the end of the constructor which doesn't really make sense.
I'm using "Writer" to communicate with a PLC (write/read data).
It has Pubic slotsClient has Signals connected to that slots
As the read or write operations can take some time,
I move a writer object to new thread to dont block the main thread.Maybe this is not the best way to do this but It is doing the job as expected.Please tell me how would you advise me to do it if you have an idea?
thx -
@LeLev
Client(){ Thread workerThread; Writer2 *writer2 = new Writer2(); writer2->moveToThread(&workerThread); }
workerThread will be destroyed as soon as the constructor finishes. That means: after construction of Client there is no thread anymore! workerThread should be member variable in Client, not a local variable in its constructor.
-
@jsulm said in Exit SLOT when destroying QThread:
Thread workerThread;
Hi,
thx !
The code i pasted is not my real code.. i have tryed to summarize but I made errors... In fact QThread workerThread is a member.. this is not my probleme.@SGaist said in Exit SLOT when destroying QThread:
As for your loop, why not use a QTimer to do the check periodically rather than that recursive loop ?
Use of QTimer solved my probleme !