指点成金-最美分享吧

登录

子线程中的 waitForReadyRead() 冻结 GUI 线程

佚名 举报

技术标签:

【中文标题】子线程中的 waitForReadyRead() 冻结 GUI 线程【英文标题】:waitForReadyRead() in subthread freezes the GUI thread 【发布时间】:2015-12-03 12:22:17 【问题描述】:

对不起我的英语不好:(

我使用了一个继承自 QObject 的名为 SerialPort 的类:

serial_port.cpp & serial_port.h:

class SerialPort: public QObject    Q_OBJECTprivate:    bool checkStatusConnected();private slots:    void readData();bool SerialPort::checkStatusConnected()    port.waitForReadyRead(200);    QBytesArray recv = port.readAll();    QString tmp = (QString)recv;    if(tmp.contains("Connected", Qt::CaseInsensitive))        return true;    return false;

MainWindow.cpp

void MainWindow::on_btnConn_clicked()    QThread* thread = new QThread(this);    serialport->moveToThread(thread);    serialport->connect();

在 SerialPort::connect() 中,我打开了端口并发送了连接命令。到现在一切正常。由于串口速度慢,我需要在下一步操作之前检查返回给我的字符串,所以在 SerialPort::connect() 中我调用 checkStatusConnected()。但是,在这个函数中,waitForReadyRead() 会冻结 GUI 线程,只要我设置的时间就会让我感到困惑。

GUI线程为什么会被另一个QThread阻塞以及如何解决这个问题?

谢谢!

【问题讨论】:

你的串口真的需要在单独的线程中运行吗? @ramtheconqueror 如果连接,我使用信号/插槽来防止 GUI 被冻结,但是连接时我想等待大约 500 毫秒的响应,然后发送数据或抛出错误。观看时,我希望 gui 正常工作,而不是被冻结。我怎么能意识到这一点? 您确定线程亲和性更改正确吗? serialport->thread()this->thread() 相比返回了什么? 【参考方案1】:

您不需要为此使用线程。 QSerialPort 是事件驱动的并且异步工作。使用计时器来查看您是否在规定的时间内收到了东西。

QSerialPort port(....);connect(&port, SIGNAL(readyRead()), this, SLOT(readFromSerialPort()));QTimer timer;timer.setInterVal(1000); // time you want to waitconnect(&timer, SIGNAL(timeout()), this, SLOT(handleTimeout()));void SomeClass::readFromSerialPort() QByteArray data(port.readAll()); // do somework

找到示例。看看这个,我认为这足以满足您的需求。

http://doc.qt.io/qt-5/qtserialport-creaderasync-example.html

【讨论】:

问题解决了!谢谢!我认为我应该在进行任何编码之前阅读更多文档。

以上是关于子线程中的 waitForReadyRead() 冻结 GUI 线程的主要内容,如果未能解决你的问题,请参考以下文章