Do not process QTimer slots while waiting for network response
-
Hello,
I have my object, subclass of QObject in a seperate thread.
This object uses QNetworkAccessManager to get data from remote server.This is how my code looks like:
auto reply = networkAccessManager.get(myRequest); QEventLoop loop; connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit); loop.exec(QEventLoop::ExcludeSocketNotifiers); doSomethngWithData(reply);But to make things more complicated, my class also contains a QTimer. And I don't want to process timeout events while waiting for data. I don't know how to do that.
I hoped QEventLoop::ExcludeSocketNotifiers would postpone timer's slot execution until after the data is loaded and processed, but it doesn't work that way.
The only solution I'm thinking about is to have a boolean isLoadingData variable and a queue for timer events, store them there, and process them afterward.
Is there a better way?
-
Hi,
Why not stop the timer when starting the request and start it again when it's done processing ?
-
Hi, thank you for your idea. That would probably work. But in my case, it is complicated. There are actually like 5 timers, some of them active and some not. Storing the active/inactive state and remaining time for each, then restoring them... I would probably create a timer subclass
PausableTimer, but it's still complicated. If there were an easier way, I would prefer it. -
Hi, thank you for your idea. That would probably work. But in my case, it is complicated. There are actually like 5 timers, some of them active and some not. Storing the active/inactive state and remaining time for each, then restoring them... I would probably create a timer subclass
PausableTimer, but it's still complicated. If there were an easier way, I would prefer it.Hi,
what about this?
auto reply = networkAccessManager.get(myRequest); const QSignalBlocker blocker(yourTimer); connect(&networkAccessManager, &QNetworkAccessManager::finished, this, [=](QNetworkReply *reply){ // blocker.unblock(); // maybe not even needed doSomethngWithData(reply); });