Solved Timer blocking applications - reactivation
-
Re: Qtimer object blocking an application.
Hello, I have a QTimer that sends a http request in timeout like this:
//in the .h file QTimer *connectionEstablishingTimer = NULL; //in the .cpp file connectionEstablishingTimer = new QTimer(this); connect(connectionEstablishingTimer,SIGNAL(timeout()),this,SLOT(EstablishConnection())); connectionEstablishingTimer->start(6000); //6 seconds //and the private slot void MainWindow::EstablishConnection(){ client->MakeRequest(methods::HEAD,json::value::null()); }
the "client" is defined as a private variable in MainWindow class.
When i runt the application and the http server is not available it turns out that that the application is blocked for about two seconds on each connectionEstablishingTimer::timeout() call (every 6 seconds) and the GUI widgets are inactive (not available to be clicked).
The client->MakeRequest() itself, works in blocking mode which obvious is the reason form blocks but searching the forum with similar topic I found the answer that each QTimer is an asynchronous timer and it is not possible that timer blocks the application. But this is happening. I'm using qt 6.02.
Do you have any Idea how to resolve this problem ? -
@Seba-the-student said in Timer blocking applications - reactivation:
The client->MakeRequest() itself, works in blocking mode which obvious is the reason
BINGO!
that each QTimer is an asynchronous timer
This refers to the passage of time between each timeout.
What you need is to move
client->MakeRequest
to a different thread or make that method work asynchronously -
@Seba-the-student said in Timer blocking applications - reactivation:
The client->MakeRequest() itself, works in blocking mode which obvious is the reason
BINGO!
that each QTimer is an asynchronous timer
This refers to the passage of time between each timeout.
What you need is to move
client->MakeRequest
to a different thread or make that method work asynchronously -
Thanks,
I made a quick update and set up a std::futere:void MainWindow::EstablishConnection(){ std::future<void> establishingThread = std::async(std::launch::async, [this]() { this->client->MakeRequest(methods::HEAD,json::value::null()); }); }
It didn't help but any way I thinks it's a good direction to follow.
Seems like I need to get deeper with QThreads.
Thanks a lot.Edit: QtConcurrent::run() did the job perfectly (https://doc.qt.io/qt-5/qtconcurrentrun.html)
-
Threads are a minefield. For example, in your code if
this
gets deleted before the other thread finishes your program would crash