在 QThread 中运行长时间操作并将信号发送到主线程仍会冻结 UI
Running a long operation in QThread and sending signals to the main thread still freezes the UI
这是我所拥有的:
void MyThread::run() {
int progress = 0;
while (1) {
// do something
progres++;
emit(progressChanged(progress));
}
}
// mainwindow
auto t = new MyTread();
connect(t, SIGNAL(progressChanged(int)), this, SLOT(onProgressChanged(int)));
t->start();
void MaiWindow::onProgressChanged(int) {
this->progressBar->setValue(progressBar->value() + 1);
}
它有效,线程中的工作完成,进度条一直上升到 100%。
但是 UI 完全冻结/滞后。拖动带有进度条的窗口会导致 5 秒的延迟。我尝试使用较低的线程优先级 - 没有结果。
也许我在这里需要一个互斥锁?
不要发出太多进度更改的信号。信号很快,但如果您每秒设置进度条值数百或数千次,UI 将冻结。将进度条更改保持在最低限度,每秒 5-10 次更改绰绰有余。
相关文章:
- 一旦双簧管录制开始,主 UI 线程就会被阻止
- QtConcurrent - 在发布到 UI 线程的数千个结果中保持 GUI 响应
- 发出信号以从线程更新 UI
- 如何在 SDI 项目中使用 UI 线程
- 如何抓取指向Qt中弹出对话框的指针,该对话框阻止了QTest中的UI线程
- 如何在不阻塞 UI 线程的情况下对C++发出 http 请求
- C++:如何在UI线程和worker std::thread之间使用std::condition_variable
- 默认情况下,从工作线程发出Qt::信号会使主线程上的UI更新?
- 如何在对话框编辑框中显示来自 UI 线程类 Run() 函数的字符串
- 多线程 UI 的最佳方式
- 线程在更新 UI 之前启动
- PPL任务何时在UI线程上执行
- Android NDK 多线程块 UI 响应
- 从命令行构建x64 C++项目说:此操作应该只在UI线程上进行
- Qt信号以明显的延迟到达UI线程
- 访问列表项时的 UI 和工作线程同步
- Gtkmm : 如何从另一个线程更新 UI?不断
- 工作线程如何与主 UI 线程通信
- 提升::线程 UI 交互
- 两个线程(COM和WPF UI)访问同一个浮点