【发布时间】:2016-12-31 14:25:13
【问题描述】:
我有一个 Qt GUI 应用程序,它正在做一些重要的实时工作,不能不惜一切代价中断这些工作(通过 LAN 转发一些传入的串行流量)。目前,当没有与 GUI 交互时,应用程序运行完美,但是一旦您单击按钮或拖动表单,似乎在处理单击时转发停止。转发是在 QTimer 循环中完成的,我已经将它放在与 GUI 线程不同的线程上,但结果没有变化。 以下是部分代码:
class MainWindow : public QMainWindow
{
QSerialPort serialReceiver; // This is the serial object
QTcpSocket *clientConnection;
}
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
// Some Initializations ...
QThread* timerthread = new QThread(this); // This is the thread that is supposed to do the forwarding
QTimer *timer = new QTimer(0);
timer->setInterval(25);
timer->moveToThread(timerthread);
connect(timer ,SIGNAL(timeout()),this,SLOT(readserialData())); // Run readserialData() each 25ms
timer->connect(timerthread, SIGNAL(started()), SLOT(start()));
timerthread->start();
}
void MainWindow::readserialData()
{
if(serialReceiver.isOpen() )
{
qint64 available = serialReceiver.bytesAvailable();
if(available > 0) // Read the serial if any data is available
{
QByteArray serialReceivedData = serialReceiver.readAll(); // This line would not be executed when there is an interaction with the GUI
if(isClientConnet)
{
int writedataNum = clientConnection->write(serialReceivedData);
}
}
}
}
正如我之前所说,这段代码在空闲情况下运行良好,没有任何数据丢失。我做错了吗?
【问题讨论】:
-
我不会亲自将 UI 和真正重要的工作混为一谈。我会考虑将 Redis 放在两者之间。
标签: c++ multithreading qt user-interface