Restart QThread with GUI

别来无恙 提交于 2019-12-24 03:12:39

问题


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()

回答1:


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.



来源:https://stackoverflow.com/questions/44006024/restart-qthread-with-gui

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!