unsync function , QSocketNotifier & Cannot move to target thread
-
@__d4ve__ said in unsync function , QSocketNotifier & Cannot move to target thread:
GUI will be unresponsive other wise
Use signals and slots and a/or a QTimer. No need to block anything.
Globlals, especially QObject one are bad design. -
Used QTimer , makes GUI freeze :
void MenuClickHandler::syncApplication() { if(Client::instance()->sync) { Client::instance()->disconnectSync(); return; } QTimer* timer = new QTimer(this); // Use a timer instead of a new thread connect(timer, &QTimer::timeout, [=](){ Client::instance()->initSync(); while (Client::instance()->sync == true) { qDebug() << "Data Receiving loop."; qDebug() << "Client->sync = " << Client::instance()->sync; Client::instance()->DataReceived(); } }); timer->start(1000); }
I can see that something wrong with the design but as far as i know Qt usage , Qthreads or instance() I would like to hear what other options there , if i Initiate object on main() , what's the right way to connect it ?
I have QAction on main()
QAction::connect(actionSync, &QAction::triggered, clickHandler, &MenuClickHandler::syncApplication);
that changes it label to 'unsync' after successful syncing another click should unsync it .
this scenario may confuse so what should be done with signal slot ?first click syncs
the second unsyncs ( on successful synced client only )
recommended not using Qthreads , Client instance() -
Used QTimer , makes GUI freeze :
void MenuClickHandler::syncApplication() { if(Client::instance()->sync) { Client::instance()->disconnectSync(); return; } QTimer* timer = new QTimer(this); // Use a timer instead of a new thread connect(timer, &QTimer::timeout, [=](){ Client::instance()->initSync(); while (Client::instance()->sync == true) { qDebug() << "Data Receiving loop."; qDebug() << "Client->sync = " << Client::instance()->sync; Client::instance()->DataReceived(); } }); timer->start(1000); }
I can see that something wrong with the design but as far as i know Qt usage , Qthreads or instance() I would like to hear what other options there , if i Initiate object on main() , what's the right way to connect it ?
I have QAction on main()
QAction::connect(actionSync, &QAction::triggered, clickHandler, &MenuClickHandler::syncApplication);
that changes it label to 'unsync' after successful syncing another click should unsync it .
this scenario may confuse so what should be done with signal slot ?first click syncs
the second unsyncs ( on successful synced client only )
recommended not using Qthreads , Client instance()@__d4ve__ said in unsync function , QSocketNotifier & Cannot move to target thread:
while (Client::instance()->sync == true) {
I mean -what do you expect when you block the event loop... let Client emit a signal when the data was received.
-
Changed the code but the GUI still not responsive :
void MenuClickHandler::syncApplication() { qDebug() << Client::instance()->socket->isOpen() ; if(Client::instance()->sync && Client::instance()->socket->isOpen() ) { Client::instance()->disconnectSync(); return; } QTimer* timer = new QTimer(this); // Use a timer instead of a new thread connect(timer, &QTimer::timeout, [=](){ Client::instance()->initSync(); while (Client::instance()->sync == true) { qDebug() << "Data Receiving loop."; qDebug() << "Client->sync = " << Client::instance()->sync; Client::instance()->DataReceived(); } }); timer->start(1000); }
Syncs successfully but not responsive afterwards
-
Changed the code but the GUI still not responsive :
void MenuClickHandler::syncApplication() { qDebug() << Client::instance()->socket->isOpen() ; if(Client::instance()->sync && Client::instance()->socket->isOpen() ) { Client::instance()->disconnectSync(); return; } QTimer* timer = new QTimer(this); // Use a timer instead of a new thread connect(timer, &QTimer::timeout, [=](){ Client::instance()->initSync(); while (Client::instance()->sync == true) { qDebug() << "Data Receiving loop."; qDebug() << "Client->sync = " << Client::instance()->sync; Client::instance()->DataReceived(); } }); timer->start(1000); }
Syncs successfully but not responsive afterwards
@__d4ve__ said in unsync function , QSocketNotifier & Cannot move to target thread:
connect(timer, &QTimer::timeout, ={
Client::instance()->initSync(); while (Client::instance()->sync == true) { qDebug() << "Data Receiving loop."; qDebug() << "Client->sync = " << Client::instance()->sync; Client::instance()->DataReceived(); } }); timer->start(1000);
I think the while loop is still blocking.
So better something like
QTimer::singleShot(0, this, ()[=]{ // sync here });
-
The problem with SingleShot function is that its good as written for 'single shot' what makes the app not responsive as desired in period of times.
I changed program design and currently sync loop runs on SingleShot but I need alternative
not using QThreads and while loop leaves me with maybe connect on some termsI have :
sync(): init connection
connection_loop() : maintains connection
receiveData() : receives data from server
-
The problem with SingleShot function is that its good as written for 'single shot' what makes the app not responsive as desired in period of times.
I changed program design and currently sync loop runs on SingleShot but I need alternative
not using QThreads and while loop leaves me with maybe connect on some termsI have :
sync(): init connection
connection_loop() : maintains connection
receiveData() : receives data from server
@__d4ve__ said in unsync function , QSocketNotifier & Cannot move to target thread:
connection_loop() : maintains connection
What does this mean? What has to be done in there? A keep-alive every n seconds? Use a QTimer.
receiveData() : receives data from server
Use the readyRead() signal and process your data.
-
I use
socket->waitForReadyRead(1000);
insteadreadyRead()
and yesconnection_loop()
does keep-alive every n seconds but I look for something more responsive@__d4ve__ said in unsync function , QSocketNotifier & Cannot move to target thread:
I use socket->waitForReadyRead(1000);
Again: do not block the event loop, use signals and slots.
-