另一个应用程序用 c++ 在 Windows 上的线程中破坏 QTimer

Another application breaks QTimer in thread on Windows with c++

本文关键字:线程 QTimer 应用程序 c++ Windows 另一个      更新时间:2023-10-16

我有自己的小框架(serial_port下面(通过QSerialPort发送/获取消息。这是我如何从另一个线程开始的:

serial_port->moveToThread(connection_thread);
serial_port->serial->moveToThread(connection_thread);
serial_port->timer->moveToThread(connection_thread);

然后我连接信号:

connect(connection_thread, SIGNAL(started()), serial_port, SLOT(start_loop()));
connect(serial_port, SIGNAL(finished()), connection_thread, SLOT(quit()));
connect(connection_thread, SIGNAL(finished()), serial_port, SLOT(deleteLater()));
connect(serial_port, SIGNAL(finished()), connection_thread, SLOT(deleteLater()));

serial_port中,QTimer 发送超时消息:

connect(timer, SIGNAL(timeout()), this, SLOT(send_message()));   

我的问题是另一个程序在QTimer开始工作后运行,正在破坏这个计时器。它发生在大约 20% 的病例中。问题包括获得信号timeout()比预期的要快得多。它发生在Windows平台上,但对于Linux来说一切都很好。我在哪里可以找到问题的原因?

尝试使用这个:

  • connection_thread->setPriority(QThread::HighestPriority);

  • connection_thread->setPriority(QThread::TimeCriticalPriority);

使计划更频繁。

  • serial_port->timer->setTimerType(Qt::PreciseTimer)

使QTimer成为PriciseTimer类型。

希望对您有所帮助。