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 terminate qthread?

how to terminate qthread?

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 4 Posters 598 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.
  • T Offline
    T Offline
    Trojan Arun
    wrote on last edited by
    #1

    if (ui->pushButton->text()=="Start Monitor") {
    ui->pushButton->setText("Stop Monitor");

       ui->pushButton->setStyleSheet("background-color: red");
       connect(thread, SIGNAL(started()), this, SLOT(doWork()));
       //this->moveToThread(thread);
       thread->start();
       connect(this,SIGNAL(valueChanged(QStringList)),this,SLOT (updateLED(QStringList)));
    
    } else {
        ui->pushButton->setText("Start Monitor");
        ui->pushButton->setStyleSheet("background-color: green");
        update_gui();
    

    //here i need to terminate the thread.
    thread->terminate();
    thread->wait(0x7fff);
    QApplication::processEvents();
    }

    void MainWindow::doWork()
    {
    while(1)
    {
    //processing some excel data and updating into buffer.
    //once the buffer received content i m calling slot to update GUI
    }
    }

    i tried using terminate(),quit().. etc to stop the thread.
    but i m getting strucked inside my while loop.
    how should i come out from that thread function while loop

    sierdzioS 1 Reply Last reply
    0
    • T Trojan Arun

      if (ui->pushButton->text()=="Start Monitor") {
      ui->pushButton->setText("Stop Monitor");

         ui->pushButton->setStyleSheet("background-color: red");
         connect(thread, SIGNAL(started()), this, SLOT(doWork()));
         //this->moveToThread(thread);
         thread->start();
         connect(this,SIGNAL(valueChanged(QStringList)),this,SLOT (updateLED(QStringList)));
      
      } else {
          ui->pushButton->setText("Start Monitor");
          ui->pushButton->setStyleSheet("background-color: green");
          update_gui();
      

      //here i need to terminate the thread.
      thread->terminate();
      thread->wait(0x7fff);
      QApplication::processEvents();
      }

      void MainWindow::doWork()
      {
      while(1)
      {
      //processing some excel data and updating into buffer.
      //once the buffer received content i m calling slot to update GUI
      }
      }

      i tried using terminate(),quit().. etc to stop the thread.
      but i m getting strucked inside my while loop.
      how should i come out from that thread function while loop

      sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      @Trojan-Arun said in how to terminate qthread?:

      how should i come out from that thread function while loop

      Use break; statement.

      What does your thread do? Is it implemented using a subclass of QThread or you use it with "worker object" approach?

      (Z(:^

      1 Reply Last reply
      0
      • T Offline
        T Offline
        Trojan Arun
        wrote on last edited by
        #3

        i cant use break inside loop; becoz unit i press "stop monitor" that dowork while loop should run.

        Christian EhrlicherC 1 Reply Last reply
        0
        • T Trojan Arun

          i cant use break inside loop; becoz unit i press "stop monitor" that dowork while loop should run.

          Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Trojan-Arun You still not show us what you're doing in your thread, what implementation version you use and why you need it at all.

          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
          0
          • T Offline
            T Offline
            Trojan Arun
            wrote on last edited by
            #5

            void MainWindow::doWork()
            {
            QString temp_filename1 = "online_status_log.csv";
            getcwd(cwd,sizeof(cwd));
            update_gui();

              QString read_val,arr_val;
              //
            
              while(1)
              {
            
                Config_File_.setFileName(temp_filename1);
                if(!Config_File_.open(QIODevice::ReadOnly))
                 {
                     qDebug("\n\tFile Not Available");
                 }
                 else
                 {
                    while(!in.atEnd())
                    {
                      read_val = in.readLine();
                       while(read_val!="")
                       {
                           arr_val = read_val;
                           read_val = in.readLine();
                       }
                         temp = arr_val.split(",");
                    }
                }
                Config_File_.close();
                qDebug("inside while condition");
                QString var_arg;
                var_arg = temp.at(2);
                if(var_arg == '0'||var_arg == '1')
                {
                    valueChanged(temp);
                    qApp->processEvents();
                    wait(500);
            
                    
                    qDebug("inside if condition");
                }
              }
              qDebug("End of the function");
            

            }

            J.HilkJ 1 Reply Last reply
            0
            • T Trojan Arun

              void MainWindow::doWork()
              {
              QString temp_filename1 = "online_status_log.csv";
              getcwd(cwd,sizeof(cwd));
              update_gui();

                QString read_val,arr_val;
                //
              
                while(1)
                {
              
                  Config_File_.setFileName(temp_filename1);
                  if(!Config_File_.open(QIODevice::ReadOnly))
                   {
                       qDebug("\n\tFile Not Available");
                   }
                   else
                   {
                      while(!in.atEnd())
                      {
                        read_val = in.readLine();
                         while(read_val!="")
                         {
                             arr_val = read_val;
                             read_val = in.readLine();
                         }
                           temp = arr_val.split(",");
                      }
                  }
                  Config_File_.close();
                  qDebug("inside while condition");
                  QString var_arg;
                  var_arg = temp.at(2);
                  if(var_arg == '0'||var_arg == '1')
                  {
                      valueChanged(temp);
                      qApp->processEvents();
                      wait(500);
              
                      
                      qDebug("inside if condition");
                  }
                }
                qDebug("End of the function");
              

              }

              J.HilkJ Online
              J.HilkJ Online
              J.Hilk
              Moderators
              wrote on last edited by
              #6

              @Trojan-Arun
              assuming valueChanged is a signal you could do the following(untested):

              QString temp_filename1;
                  
                  QThread *worker = QThread::create([=]()->void{
                      QString read_val,arr_val;
                      while(1)
                      {
                          QFile Config_File_;
                          QStringList temp;
                          Config_File_.setFileName(temp_filename1);
                          QTextStream in(&Config_File_);
                          if(!Config_File_.open(QIODevice::ReadOnly))
                          {
                              qDebug("\n\tFile Not Available");
                          }
                          else
                          {
                              while(!in.atEnd())
                              {
                                  read_val = in.readLine();
                                  while(read_val!="")
                                  {
                                      arr_val = read_val;
                                      read_val = in.readLine();
                                  }
                                  temp = arr_val.split(",");
                              }
                          }
                          Config_File_.close();
                          qDebug("inside while condition");
                          QString var_arg;
                          var_arg = temp.at(2);
                          if(var_arg == '0'||var_arg == '1')
                          {
                              valueChanged(temp);
                              qDebug("inside if condition");
                          }
                          break;
                      }
                      qDebug("End of the function");
                  });
                  connect(this, &MainWindow::valueChanged, worker, &QThread::quit);
                  connect(worker, &QThread::finished, worker, &QThread::deleteLater);
                  worker->start();
              

              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

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

                @Trojan-Arun said in how to terminate qthread?:

                connect(thread, SIGNAL(started()), this, SLOT(doWork()));

                Now I see it - how should this ever work? I mean this is living in the main thread so doWork() will also be executed in the main thread. The QThread documentation has a lot of examples how to do it right...

                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
                • J.HilkJ J.Hilk

                  @Trojan-Arun
                  assuming valueChanged is a signal you could do the following(untested):

                  QString temp_filename1;
                      
                      QThread *worker = QThread::create([=]()->void{
                          QString read_val,arr_val;
                          while(1)
                          {
                              QFile Config_File_;
                              QStringList temp;
                              Config_File_.setFileName(temp_filename1);
                              QTextStream in(&Config_File_);
                              if(!Config_File_.open(QIODevice::ReadOnly))
                              {
                                  qDebug("\n\tFile Not Available");
                              }
                              else
                              {
                                  while(!in.atEnd())
                                  {
                                      read_val = in.readLine();
                                      while(read_val!="")
                                      {
                                          arr_val = read_val;
                                          read_val = in.readLine();
                                      }
                                      temp = arr_val.split(",");
                                  }
                              }
                              Config_File_.close();
                              qDebug("inside while condition");
                              QString var_arg;
                              var_arg = temp.at(2);
                              if(var_arg == '0'||var_arg == '1')
                              {
                                  valueChanged(temp);
                                  qDebug("inside if condition");
                              }
                              break;
                          }
                          qDebug("End of the function");
                      });
                      connect(this, &MainWindow::valueChanged, worker, &QThread::quit);
                      connect(worker, &QThread::finished, worker, &QThread::deleteLater);
                      worker->start();
                  
                  T Offline
                  T Offline
                  Trojan Arun
                  wrote on last edited by
                  #8

                  @J-Hilk said in how to terminate qthread?:

                  QString temp_filename1;

                  The above example will work on every button press event. but my requirement is untill i press "stop monitor", that loop should execute continously.

                  J.HilkJ 1 Reply Last reply
                  0
                  • T Trojan Arun

                    @J-Hilk said in how to terminate qthread?:

                    QString temp_filename1;

                    The above example will work on every button press event. but my requirement is untill i press "stop monitor", that loop should execute continously.

                    J.HilkJ Online
                    J.HilkJ Online
                    J.Hilk
                    Moderators
                    wrote on last edited by
                    #9

                    @Trojan-Arun
                    than you'll have to(should) do it properly, with a worker class, and a QThread

                    onButton pressed you then simply call start on the thread and on 2nd press you call stop.

                    You'll have to remove my deleteLater connect and manage that yourself in the destructor of your parent class.


                    Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                    Q: What's that?
                    A: It's blue light.
                    Q: What does it do?
                    A: It turns blue.

                    1 Reply Last reply
                    1

                    • Login

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