[英]QThread stuck in second run
I have a QThread on my embedded device. 我的嵌入式设备上有一个QThread。 every time I run the application my thread stuck after second run.
每次我运行该应用程序时,我的线程都会在第二次运行后卡住。 I try to kill my thread after first run.
我尝试在第一次运行后杀死线程。 Still device stuck after second run.
第二次运行后,设备仍然卡住。 I couldn't run my thread correctly.
我无法正确运行线程。
Here is my code; 这是我的代码;
void ThreadCurrency::run()
{
QMutex mutex;
mutex.lock();
if(this->CurrencyStop == true)
{
mutex.unlock();
return;
}
QByteArray strdata;
// Create QProcess object
processCurrency = new QProcess();
processCurrency->start("curl --insecure -v --cacert /data/ca/cert.pem https://secure.*******************/fx.jsp");
if (processCurrency->waitForStarted(-1))
{
while(processCurrency->waitForReadyRead(-1))
{
strdata += processCurrency->readAllStandardOutput();
}
QMessageBox msgBox1;
msgBox1.setWindowTitle("eCode Read");
msgBox1.setText(strdata);
msgBox1.exec();
}
else
{
while(processCurrency->waitForReadyRead(-1))
{
strdata += processCurrency->readAllStandardError();
}
QMessageBox msgBox1;
msgBox1.setWindowTitle("eCode Error");
msgBox1.setText(strdata);
msgBox1.exec();
}
mutex.unlock();
sleep(1);
//*****************************************************************
emit CurrencyChanged(aGBP, aEUR, aUSD, sGBP, sEUR, sUSD);
}
** The Output shows in a json format:** **输出以json格式显示:**
{
"date": "20171107", "currency": {
"dolar": {
"buy": "3,8200",
"sale": "3,9050",
"e_sale": "3,8200"
},
}
}
Thank you for suggestion. 谢谢你的建议。 The QMutexLocker can't work in my embedded device.
QMutexLocker无法在我的嵌入式设备中工作。 QMutex and the QThread is the closes I can get in my device.
QMutex和QThread是我可以在设备中获得的关闭。 My problem is the below line: emit CurrencyChanged(aGBP, aEUR, aUSD, sGBP, sEUR, sUSD);
我的问题是以下行:发出CurrencyChanged(aGBP,aEUR,aUSD,sGBP,sEUR,sUSD); The line runs but It doesn't fire onCurrencyChanged SLOT.
该行运行,但是不会在onCurrencyChanged SLOT上触发。 Anything in onCurrencyChanged doesn't run.
onCurrencyChanged中的任何内容均不会运行。 My main thread code is:
我的主线程代码是:
currencyThread = new ThreadCurrency (this);
connect(currencyThread,SIGNAL(CurrencyChanged(QString, QString, QString, QString, QString, QString)), this, SLOT(onCurrencyChanged (QString, QString, QString, QString, QString, QString)));
currencyThread->CurrencyStop = false;
currencyTimer = new QTimer(this);
connect(currencyTimer, SIGNAL(timeout()),this, SLOT(showCurrencyStatus()));
currencyTimer->start(30000);
void MainWindow::onCurrencyChanged(QString aGBP, QString aEUR, QString aUSD, QString sGBP, QString sEUR, QString sUSD)
{
// SHOW Currency
ui->lblALIS_STG->setText(aGBP);
ui->lblALIS_EUR->setText(aEUR);
ui->lblALIS_USD->setText(aUSD);
QCoreApplication::processEvents();
}
1) You are not allowed to access Widgets in a thread besides the main thread. 1)除主线程外,不允许访问其他线程中的小部件。 Thus move all
QMessageBox
code to the main thread (eg after receiving the CurrencyChanged
signal). 因此,将所有
QMessageBox
代码移至主线程(例如,在收到CurrencyChanged
信号之后)。
2) Make sure, that the CurrencyChanged
signal is connected using a QueuedConnection
, otherwise, your GUI will be executed inside the secondary worker thread. 2)确保使用
QueuedConnection
连接了CurrencyChanged
信号,否则,您的GUI将在辅助工作线程内执行。
3) Do not use QMutex
directly, use a QMutexLocker
instead 3)不要直接使用
QMutex
,请改用QMutexLocker
4) Why do you need the Mutex anyhow? 4)为什么您仍然需要互斥体? Locking based on (arbitrary) input thus arbitrary time is close to deadlocking your application.
基于(任意)输入的锁定因此任意时间都接近死锁您的应用程序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.