使用 GUI 重启 QThread [英] Restart QThread with GUI

查看:65
本文介绍了使用 GUI 重启 QThread的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 QThread 在单独的线程中进行一些计算.通过单击按钮启动线程,启动函数 StartMeasurement().线程可以自己完成这个过程(完成计算后)并发出 PyQT 信号完成.或者用户可以通过单击 stopBtn 来停止线程.

terminate() 函数正在工作,但是当我再次尝试启动线程时遇到很多麻烦.

在这里使用 movetoThread() 方法是否值得推荐?或者我如何确保正确停止线程以启用正确的重启.(意思是,开始新的!)

在线程中开始测量:StartMeasurement()

def StartMeasurement(self):self.thread = measure.CMeasurementThread(self.osziObj, self.genObj, self.measSetup)self.thread.newSample.connect(self.plotNewSample)self.thread.finished.connect(self.Done)self.stopBtn.clicked.connect(self.thread.terminate)self.stopBtn.clicked.connect(self.Stop)self.thread.start()

解决方案

这不是问题.使用 QThread 时的一般做法是将其 finished() 信号连接到已移动到的对象的 deleteLater() 槽通过 moveToThread() 分离线程.这样做是为了在您销毁线程时正确管理内存,因为假设您将首先退出线程然后销毁其实例.;) 这应该告诉您停止线程与销毁这些对象无关,除非您已经建立了我上面描述的连接.

如果您已经使用 quit()wait() 正确地停止了一个线程,那么重新启动它是完全没问题的,实际上等待直到停止完成.>

但是我的建议是不要这样做,除非由于某种原因额外的线程对您的应用程序产生了巨大影响(现代机器极不可能).

不要重新启动线程,请考虑以下选项:

  • 实现一个 pause 标志,如果它设置为 true(我使用了 this 我的这个例子多次证明了这种行为(检查 worker.cpp尤其是 doWork() 函数) - 它是在 C++ 中,但它可以立即移植到 PyQt)
  • use QRunnable - 它旨在运行某些东西,然后(除非 autoDelete 设置为 true)返回到线程池.如果您的任务每隔一段时间发生一次,并且您不需要持续运行单独的线程,那就太好了.如果你想使用信号和槽(为了获得在 QRunnable::run() 中完成的计算结果,你必须首先从 QObject 继承,然后从QRunnable
  • 使用期货(查看 Qt Concurrent 模块)

建议先阅读示例使用Qt 提供的各种线程技术的案例.

I am using QThread to do some calculations in a seperate Thread. The Thread get's started by a button click, witch launches the function StartMeasurement(). The Thread can finish the process by itself (after finished the calculations) and emits the PyQT Signal finished. Or the thread can be stopped by the User by the stopBtn click.

The terminate() function is working, but I get a lot of troubles when I try to start the thread again.

Is it recommendable to use the movetoThread() approach here? Or how could I ensure that the thread is stopped correctly to enable a proper restart. (means, starting new!)

starts the measurment in a Thread: StartMeasurement()

def StartMeasurement(self):            

    self.thread = measure.CMeasurementThread(self.osziObj, self.genObj, self.measSetup)

    self.thread.newSample.connect(self.plotNewSample)
    self.thread.finished.connect(self.Done)
    self.stopBtn.clicked.connect(self.thread.terminate)
    self.stopBtn.clicked.connect(self.Stop)

    self.thread.start()

解决方案

It's not a problem. The general practice when working with QThread is to connect its finished() signal to the deleteLater() slot of the objects that have been moved to the separate thread via moveToThread(). It's done in order to properly manage the memory when you then destroy your thread because it's assumed that you will first quit the thread and then destroy its instance. ;) This should tell you that stopping a thread has nothing to do with the destruction of those objects UNLESS you have established the connection I've described above.

It is perfectly fine to restart a thread IF you have stopped it properly using quit() and wait() to actually wait untill the stopping is completed.

However my advice is to not do that unless that extra thread has a huge impact on your application for some reason (highly unlikely with modern machines).

Instead of restarting the thread consider the following options:

  • implement a pause flag that just makes the thread run without doing anything if it's set to true (I've used this example of mine many times to demonstrate such behaviour (check the worker.cpp and the doWork() function in particular) - it's in C++ but it can be ported to PyQt in no time)
  • use QRunnable - its designed to run something and then (unless autoDelete is set to true) return to the thread pool. It's really nice if you have tasks that occur every once in a while and you don't need a constatly running separate thread. If you want to use signals and slots (to get the result of the calculation done inside the QRunnable::run() you will have to first inherit from QObject and then from QRunnable
  • Use futures (check the Qt Concurrent module)

I suggest that you first read the Example use cases for the various threading technologies Qt provides.

这篇关于使用 GUI 重启 QThread的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆