Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Timer blocking applications - reactivation
Forum Updated to NodeBB v4.3 + New Features

Timer blocking applications - reactivation

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 491 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Seba the student
    wrote on last edited by
    #1

    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 ?

    VRoninV 1 Reply Last reply
    0
    • S Seba the student

      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 ?

      VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      @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

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      2
      • S Offline
        S Offline
        Seba the student
        wrote on last edited by Seba the student
        #3

        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)

        1 Reply Last reply
        0
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #4

          Threads are a minefield. For example, in your code if this gets deleted before the other thread finishes your program would crash

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          1 Reply Last reply
          2

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved