the main UI thread will die when no operation for a long time
-
I have developed a qt5 application which has three threads: thread 1 and thread 2 will get data from serial port and thread 3 updates UI with these data periodly.
Now I found a problem that when I leave the application for a long time(about 1 hour), the main UI thread will die, and OS will ask you whether force quit or wait.
I'm sure that the other thow seiral port reader thred is alive and the ui thread is dead by analyzing the console output log.
Maybe there is some porblem with my code or it's a BUG or something else? My OS is Centos 7.5.forllowing is some part of my code:
void MainWindow::startUIThread(){ myUITreead = new MyUIThread(this); myUITreead->start(); connect(myUITreead, SIGNAL(updateUI()), this, SLOT(updateUI())); } void MainWindow::updateUI(){ QString stringLog = "["+QDateTime::currentDateTime().toString()+"] "; int ret; QString temStr; pid_t pid; pthread_t tid; pid = getpid(); tid = pthread_self(); cout<<"MainWindow->updateUI() ("<<pid<<","<<tid<<") ["+QDateTime::currentDateTime().toString().toStdString()+"]"<<endl; #ifdef SERIAL_PORT_MODE if(serialPortMaster!=nullptr){ ret = serialPortMaster->getPackage(packageM); if(ret!=0){ cout<<"MainWindow->updateUI() ("<<pid<<","<<tid<<") ["+QDateTime::currentDateTime().toString().toStdString()+"]"<<"serialPortMaster->getPackage failed"<<endl; }else{ if(labelInputLedStatus){ labelInputLed->setStyleSheet("QLabel {background-color: green;color:white;}"); }else{ labelInputLed->setStyleSheet("QLabel {background-color: red;color:white;}"); } labelInputLedStatus = !labelInputLedStatus; cout<<"MainWindow->updateUI() ("<<pid<<","<<tid<<") ["+QDateTime::currentDateTime().toString().toStdString()+"]"<<"updating..."<<endl; lineEditWheelAngle->setText(temStr.setNum(packageM[1]*1800/10000-900)); lineEditBrakeStength->setText(temStr.setNum(packageM[2]/20)); float f = packageM[3]-370; f /=2500-370; f *=100; lineEditAcceleratorDegree->setText(temStr.setNum(f)); lineEditWheelVoltage->setText(temStr.setNum(((float)packageM[1]/1000))); lineEditAcceleratorVoltage1->setText(temStr.setNum(((float)packageM[3])/1000)); lineEditAcceleratorVoltage2->setText(temStr.setNum(((float)packageM[4])/1000)); lineEditBrakeVoltage->setText(temStr.setNum(((float)packageM[2])/1000)); temStr.sprintf("%d %d %f %d %d %d %d ",packageM[1]*1800/10000-900,packageM[2]/10,f,packageM[1],packageM[3],packageM[4],packageM[2]); stringLog +=temStr; } } cout<<"MainWindow->updateUI()1 ("<<pid<<","<<tid<<") ["+QDateTime::currentDateTime().toString().toStdString()+"]"<<endl; if(serialPortSlave!=nullptr){ ret = serialPortSlave->getPackage(packageS); if(ret!=0){ }else{ if(labelOutputLedStatus){ labelOutputLed->setStyleSheet("QLabel {background-color: green;color:white;}"); }else{ labelOutputLed->setStyleSheet("QLabel {background-color: red;color:white;}"); } labelOutputLedStatus = !labelOutputLedStatus; lineEditWheelAngleFeedback->setText(temStr.setNum((5000-packageS[1])*900/5000)); lineEditEngineSpeedFeedback->setText(temStr.setNum(packageS[4])); lineEditCarSpeedFeedback->setText(temStr.setNum(packageS[3])); lineEditLongitude->setText(temStr.setNum(0)); lineEditLatitude->setText(temStr.setNum(0)); lineEditHigh->setText(temStr.setNum(0)); temStr.sprintf("%d %d %d %d %d %d\n",packageS[1],packageS[4],packageS[3],0,0,0); stringLog +=temStr; } } stringLog+="\n"; textEditLog->append(stringLog); if(recording){ QFile f(LOG_FILE_NAME); if(!f.open(QIODevice::WriteOnly|QIODevice::Text|QIODevice::Append)){ textEditLog->append("Open log file failed\n"); }else{ QTextStream logOutput(&f); logOutput<<stringLog; } f.close(); } cout<<"MainWindow->updateUI()0 ("<<pid<<","<<tid<<") ["+QDateTime::currentDateTime().toString().toStdString()+"]"<<endl; }``` code_text
#include "myuithread.h"
MyUIThread::MyUIThread(MainWindow* mw)
{
this->mw = mw;
running = true;
//connect(this, SIGNAL(updateUI()), this, SLOT(slotUpdateUI()));
}void MyUIThread::run()
{
pid_t pid;
pthread_t tid;
pid = getpid();
tid = pthread_self();while (running) { cout<<"MyUIThread ("<<pid<<","<<tid<<") ["+QDateTime::currentDateTime().toString().toStdString()+"]"<<endl; emit updateUI(); usleep( (1000*1000/mw->refreshRate)>10*1000 ? (1000*1000/mw->refreshRate) : 10*1000); }
}
void MyUIThread::slotUpdateUI(){
mw->updateUI();
} -
I have developed a qt5 application which has three threads: thread 1 and thread 2 will get data from serial port and thread 3 updates UI with these data periodly.
Now I found a problem that when I leave the application for a long time(about 1 hour), the main UI thread will die, and OS will ask you whether force quit or wait.
I'm sure that the other thow seiral port reader thred is alive and the ui thread is dead by analyzing the console output log.
Maybe there is some porblem with my code or it's a BUG or something else? My OS is Centos 7.5.forllowing is some part of my code:
void MainWindow::startUIThread(){ myUITreead = new MyUIThread(this); myUITreead->start(); connect(myUITreead, SIGNAL(updateUI()), this, SLOT(updateUI())); } void MainWindow::updateUI(){ QString stringLog = "["+QDateTime::currentDateTime().toString()+"] "; int ret; QString temStr; pid_t pid; pthread_t tid; pid = getpid(); tid = pthread_self(); cout<<"MainWindow->updateUI() ("<<pid<<","<<tid<<") ["+QDateTime::currentDateTime().toString().toStdString()+"]"<<endl; #ifdef SERIAL_PORT_MODE if(serialPortMaster!=nullptr){ ret = serialPortMaster->getPackage(packageM); if(ret!=0){ cout<<"MainWindow->updateUI() ("<<pid<<","<<tid<<") ["+QDateTime::currentDateTime().toString().toStdString()+"]"<<"serialPortMaster->getPackage failed"<<endl; }else{ if(labelInputLedStatus){ labelInputLed->setStyleSheet("QLabel {background-color: green;color:white;}"); }else{ labelInputLed->setStyleSheet("QLabel {background-color: red;color:white;}"); } labelInputLedStatus = !labelInputLedStatus; cout<<"MainWindow->updateUI() ("<<pid<<","<<tid<<") ["+QDateTime::currentDateTime().toString().toStdString()+"]"<<"updating..."<<endl; lineEditWheelAngle->setText(temStr.setNum(packageM[1]*1800/10000-900)); lineEditBrakeStength->setText(temStr.setNum(packageM[2]/20)); float f = packageM[3]-370; f /=2500-370; f *=100; lineEditAcceleratorDegree->setText(temStr.setNum(f)); lineEditWheelVoltage->setText(temStr.setNum(((float)packageM[1]/1000))); lineEditAcceleratorVoltage1->setText(temStr.setNum(((float)packageM[3])/1000)); lineEditAcceleratorVoltage2->setText(temStr.setNum(((float)packageM[4])/1000)); lineEditBrakeVoltage->setText(temStr.setNum(((float)packageM[2])/1000)); temStr.sprintf("%d %d %f %d %d %d %d ",packageM[1]*1800/10000-900,packageM[2]/10,f,packageM[1],packageM[3],packageM[4],packageM[2]); stringLog +=temStr; } } cout<<"MainWindow->updateUI()1 ("<<pid<<","<<tid<<") ["+QDateTime::currentDateTime().toString().toStdString()+"]"<<endl; if(serialPortSlave!=nullptr){ ret = serialPortSlave->getPackage(packageS); if(ret!=0){ }else{ if(labelOutputLedStatus){ labelOutputLed->setStyleSheet("QLabel {background-color: green;color:white;}"); }else{ labelOutputLed->setStyleSheet("QLabel {background-color: red;color:white;}"); } labelOutputLedStatus = !labelOutputLedStatus; lineEditWheelAngleFeedback->setText(temStr.setNum((5000-packageS[1])*900/5000)); lineEditEngineSpeedFeedback->setText(temStr.setNum(packageS[4])); lineEditCarSpeedFeedback->setText(temStr.setNum(packageS[3])); lineEditLongitude->setText(temStr.setNum(0)); lineEditLatitude->setText(temStr.setNum(0)); lineEditHigh->setText(temStr.setNum(0)); temStr.sprintf("%d %d %d %d %d %d\n",packageS[1],packageS[4],packageS[3],0,0,0); stringLog +=temStr; } } stringLog+="\n"; textEditLog->append(stringLog); if(recording){ QFile f(LOG_FILE_NAME); if(!f.open(QIODevice::WriteOnly|QIODevice::Text|QIODevice::Append)){ textEditLog->append("Open log file failed\n"); }else{ QTextStream logOutput(&f); logOutput<<stringLog; } f.close(); } cout<<"MainWindow->updateUI()0 ("<<pid<<","<<tid<<") ["+QDateTime::currentDateTime().toString().toStdString()+"]"<<endl; }``` code_text
#include "myuithread.h"
MyUIThread::MyUIThread(MainWindow* mw)
{
this->mw = mw;
running = true;
//connect(this, SIGNAL(updateUI()), this, SLOT(slotUpdateUI()));
}void MyUIThread::run()
{
pid_t pid;
pthread_t tid;
pid = getpid();
tid = pthread_self();while (running) { cout<<"MyUIThread ("<<pid<<","<<tid<<") ["+QDateTime::currentDateTime().toString().toStdString()+"]"<<endl; emit updateUI(); usleep( (1000*1000/mw->refreshRate)>10*1000 ? (1000*1000/mw->refreshRate) : 10*1000); }
}
void MyUIThread::slotUpdateUI(){
mw->updateUI();
}Hi, and welcome!
@weiguixm said in the main UI thread will die when no operation for a long time:
thread 3 updates UI with these data periodly
Makre sure you only update your UI from the thread that created
QApplication
.