how to terminate qthread?
-
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 -
@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?
-
i cant use break inside loop; becoz unit i press "stop monitor" that dowork while loop should run.
-
@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.
-
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");
}
-
@Trojan-Arun
assumingvalueChanged
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();
-
@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...
-
@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.
-
@Trojan-Arun
than you'll have to(should) do it properly, with a worker class, and a QThreadonButton 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.