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 QFuture thread

How to stop QFuture thread

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 2 Posters 3.5k 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.
  • Ketan__Patel__0011K Offline
    Ketan__Patel__0011K Offline
    Ketan__Patel__0011
    wrote on last edited by
    #1

    Hello experts

    i was create a small threading application , in this application one function is run parallelly

    i am using QFuture and QConcurrent

    my problem in when i try to stop qfuture that time it can't be stop

    my code is :

                        void Mainwindow::Function_1()                        
                        {                        
                            for(int i=1 ; i <= 10 ;i++)                        
                            {                                        
                                QThread::sleep(1);                        
                                ui->label->setText(QString::number(i));                        
                            }                       
                        }
    
                       ////////////// Start ////////////
                       void Mainwindow::on_start_Button_clicked()                       
                       {                                 
                           future_1 = QtConcurrent::run(this,&Mainwindow::Function_1);                       
                       }
                      ////////////// Stop////////////
                      void Mainwindow::on_stop_Button_clicked()                      
                      {                                       
                          if(this->future_1.isRunning())                      
                          {                      
                              this->future_1.cancel();                      
                              this->future_1.setPaused(true);                                   
                          }                      
                      }
    

    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    i read some documentation online but i can't get solution

    i am not able to stop qfuture thread between the running process
    please Help me to solve this problem

    Thanks in advance

    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    1 Reply Last reply
    0
    • B Offline
      B Offline
      Bonnie
      wrote on last edited by Bonnie
      #2

      QFurture cannot stop untill the fuction is completely executed...

      Be aware that not all running asynchronous computations can be canceled. For example, the future returned by QtConcurrent::run() cannot be canceled;

      QFuture::cancel() does not stop the thread immediately, it just cancels all the unstarted jobs.

      Ketan__Patel__0011K 1 Reply Last reply
      1
      • B Bonnie

        QFurture cannot stop untill the fuction is completely executed...

        Be aware that not all running asynchronous computations can be canceled. For example, the future returned by QtConcurrent::run() cannot be canceled;

        QFuture::cancel() does not stop the thread immediately, it just cancels all the unstarted jobs.

        Ketan__Patel__0011K Offline
        Ketan__Patel__0011K Offline
        Ketan__Patel__0011
        wrote on last edited by
        #3

        @Bonnie
        thanks for the replay

        but any how i want to stop it

        is it have any other way to stop any process when it is running ?

        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        please help me to solve the problem
        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        B 1 Reply Last reply
        0
        • Ketan__Patel__0011K Ketan__Patel__0011

          @Bonnie
          thanks for the replay

          but any how i want to stop it

          is it have any other way to stop any process when it is running ?

          ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
          please help me to solve the problem
          ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

          B Offline
          B Offline
          Bonnie
          wrote on last edited by Bonnie
          #4

          @Ketan__Patel__0011

          You can considering doing the normal way to stop a loop in another thread: adding a member variable to control the loop.

          bool b_exitloop = false;
          
          for(int i=1 ; i <= 10 && !b_exitloop;i++)                        
          {
              QThread::sleep(1);
              ui->label->setText(QString::number(i));
          }  
          
          if(this->future_1.isRunning())
          {
            this->b_exitloop  = true;
            this->future_1.waitForFinished();
            this->b_exitloop = false;
          }
          

          But this cannot stop the sleep.
          Also be careful about synchronizing threads if you need to.

          Ketan__Patel__0011K 1 Reply Last reply
          1
          • B Bonnie

            @Ketan__Patel__0011

            You can considering doing the normal way to stop a loop in another thread: adding a member variable to control the loop.

            bool b_exitloop = false;
            
            for(int i=1 ; i <= 10 && !b_exitloop;i++)                        
            {
                QThread::sleep(1);
                ui->label->setText(QString::number(i));
            }  
            
            if(this->future_1.isRunning())
            {
              this->b_exitloop  = true;
              this->future_1.waitForFinished();
              this->b_exitloop = false;
            }
            

            But this cannot stop the sleep.
            Also be careful about synchronizing threads if you need to.

            Ketan__Patel__0011K Offline
            Ketan__Patel__0011K Offline
            Ketan__Patel__0011
            wrote on last edited by
            #5

            @Bonnie

            Thanks for reply

            i also try this way and i get good result

            but this is not a proper and good way for any large application and sensitive application

            i need a proper way to solve this problem

            i am also doing research for this

            B 1 Reply Last reply
            0
            • Ketan__Patel__0011K Ketan__Patel__0011

              @Bonnie

              Thanks for reply

              i also try this way and i get good result

              but this is not a proper and good way for any large application and sensitive application

              i need a proper way to solve this problem

              i am also doing research for this

              B Offline
              B Offline
              Bonnie
              wrote on last edited by Bonnie
              #6

              @Ketan__Patel__0011 Well, another way is to not using loop in you function and not using run. Try using those map functions in QtConcurrent.
              Then you can cancel it.

              Ketan__Patel__0011K 1 Reply Last reply
              0
              • B Bonnie

                @Ketan__Patel__0011 Well, another way is to not using loop in you function and not using run. Try using those map functions in QtConcurrent.
                Then you can cancel it.

                Ketan__Patel__0011K Offline
                Ketan__Patel__0011K Offline
                Ketan__Patel__0011
                wrote on last edited by
                #7

                @Bonnie
                thanks for reply

                without looping statements is not reliable to manage lot's of process
                sorry but i can't remove looping statements

                1 Reply Last reply
                0
                • B Offline
                  B Offline
                  Bonnie
                  wrote on last edited by Bonnie
                  #8

                  Then I think QtConcurrent is not the right solution for you when you need to exit a loop.
                  The only way to exit a loop is still controlling by a variable.

                  Ketan__Patel__0011K 1 Reply Last reply
                  0
                  • B Bonnie

                    Then I think QtConcurrent is not the right solution for you when you need to exit a loop.
                    The only way to exit a loop is still controlling by a variable.

                    Ketan__Patel__0011K Offline
                    Ketan__Patel__0011K Offline
                    Ketan__Patel__0011
                    wrote on last edited by
                    #9

                    @Bonnie

                    Let's suppose if i use variable for controlling then when i false the any variable or boolean flag that time
                    is memory still consuming or not ?

                    1 Reply Last reply
                    0
                    • B Offline
                      B Offline
                      Bonnie
                      wrote on last edited by
                      #10

                      What memory do you mean?

                      Ketan__Patel__0011K 1 Reply Last reply
                      0
                      • B Bonnie

                        What memory do you mean?

                        Ketan__Patel__0011K Offline
                        Ketan__Patel__0011K Offline
                        Ketan__Patel__0011
                        wrote on last edited by Ketan__Patel__0011
                        #11

                        @Bonnie

                        if i stop the any function process using any boolean flag

                        that time is memory(RAM) are still consuming or not ?

                        and CPU are still used ?

                        1 Reply Last reply
                        0
                        • B Offline
                          B Offline
                          Bonnie
                          wrote on last edited by
                          #12

                          If you exit the loop, the code in the loop will stop being executed.
                          I don't understand what memeory / CPU are you talking about.

                          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