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. How to stop a delay started inside QtConcurrent thread ?
Forum Update on Monday, May 27th 2025

How to stop a delay started inside QtConcurrent thread ?

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 4 Posters 949 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.
  • xenovasX Offline
    xenovasX Offline
    xenovas
    wrote on last edited by
    #1

    Hi,

    i have some code running in a thread using QtConcurrent and i also use

    QThread::currentThread()->msleep(650000);
    

    in order to delay a while loop for about 10 mins.. but i want to stop this delay when the application ends. could you please provide some thoughts ?

    @xen0vas

    JonBJ 1 Reply Last reply
    0
    • fcarneyF Offline
      fcarneyF Offline
      fcarney
      wrote on last edited by
      #2

      Create a loop inside your thread:

      int count = 0;
      while(sleeping){
        QThread::currentThread()->msleep(1000);
      
        // process events like signals
        QCoreApplication::processEvents();  
      
        count++;
        if(count>=650){
          sleeping = false;
        }
      }
      

      Put a slot in your thread that will set sleeping to false when you want to end early. You need to process
      events in order to handle a signal. At most you will wait 1 second for this to quit. Use finer grain sleep if you desire more granularity.

      C++ is a perfectly valid school of magic.

      xenovasX 1 Reply Last reply
      2
      • fcarneyF fcarney

        Create a loop inside your thread:

        int count = 0;
        while(sleeping){
          QThread::currentThread()->msleep(1000);
        
          // process events like signals
          QCoreApplication::processEvents();  
        
          count++;
          if(count>=650){
            sleeping = false;
          }
        }
        

        Put a slot in your thread that will set sleeping to false when you want to end early. You need to process
        events in order to handle a signal. At most you will wait 1 second for this to quit. Use finer grain sleep if you desire more granularity.

        xenovasX Offline
        xenovasX Offline
        xenovas
        wrote on last edited by
        #3

        @fcarney Thank you this helped me a lot!

        @xen0vas

        1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @xenovas said in How to stop a delay started inside QtConcurrent thread ?:

          in order to delay a while loop for about 10 mins

          Use signals and slots instead - this is not the way to go.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          4
          • xenovasX xenovas

            Hi,

            i have some code running in a thread using QtConcurrent and i also use

            QThread::currentThread()->msleep(650000);
            

            in order to delay a while loop for about 10 mins.. but i want to stop this delay when the application ends. could you please provide some thoughts ?

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #5

            @xenovas
            Suggest you heed @Christian-Ehrlicher's post. Using threads/sleep/processEvents is not a good way to go when signals/slots are on offer.

            1 Reply Last reply
            0
            • xenovasX Offline
              xenovasX Offline
              xenovas
              wrote on last edited by
              #6

              @jonB @Christian-Ehrlicher i will try to use signals and slots thank you

              @xen0vas

              1 Reply Last reply
              0

              • Login

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