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 Updated to NodeBB v4.3 + New Features

How to stop a delay started inside QtConcurrent thread ?

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 4 Posters 1.0k 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.
  • X Offline
    X Offline
    xenovas
    wrote on 19 Apr 2019, 12:44 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

    J 1 Reply Last reply 20 Apr 2019, 09:35
    0
    • F Offline
      F Offline
      fcarney
      wrote on 19 Apr 2019, 14:31 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.

      X 1 Reply Last reply 20 Apr 2019, 08:38
      2
      • F fcarney
        19 Apr 2019, 14:31

        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.

        X Offline
        X Offline
        xenovas
        wrote on 20 Apr 2019, 08:38 last edited by
        #3

        @fcarney Thank you this helped me a lot!

        @xen0vas

        1 Reply Last reply
        0
        • C Offline
          C Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 20 Apr 2019, 09:21 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
          • X xenovas
            19 Apr 2019, 12:44

            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 ?

            J Offline
            J Offline
            JonB
            wrote on 20 Apr 2019, 09:35 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
            • X Offline
              X Offline
              xenovas
              wrote on 20 Apr 2019, 09:54 last edited by
              #6

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

              @xen0vas

              1 Reply Last reply
              0

              6/6

              20 Apr 2019, 09:54

              • Login

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