繁体   English   中英

pyqt:如何正确退出线程

[英]pyqt: How to quit a thread properly

我写了一个pyqt gui并使用线程来运行需要很长时间才能执行的代码,但是我想选择安全地停止执行。 我不想使用get_thread.terminate()方法。 我想通过特殊功能(也许是del ())停止代码。 我的问题是,我在自己的类中编写了代码,只是想在不更改大量语法的情况下中止该类。

编辑:有人提到必须将一个标志传递给该类,该标志必须不断检查。 如何将此标志发送给班级? 因为标志必须更改值,所以当按下停止按钮时。

编辑2:到目前为止,我的解决方案是派生一个名称为running_global的全局变量。 我将self.get_thread.terminate()更改为running_global = False,并在long_running_prog中不断检查变量是否设置为False。 我认为这种解决方案很难看,所以如果有人有更好的主意,我会很高兴。

这是我在其中启动线程的对话框的代码:

class SomeDialog(QtGui.QDialog,
     userinterface_status.Ui_status_window):
     finished = QtCore.pyqtSignal(bool)

    def __init__(self):
        """
        :param raster: Coordinates which are going to be scanned.
        """
        super(self.__class__, self).__init__()  # old version, used in python 2.
        self.setupUi(self)  # It sets up layout and widgets that are defined
        self.get_thread = SomeThread()

        # Conencting the buttons
        self.start_button.clicked.connect(self.start)
        self.stop_button.clicked.connect(self.stop)
        self.close_button.clicked.connect(self.return_main)

        # Connecting other signals
        self.connect(self.get_thread, QtCore.SIGNAL("stop()"), self.stop)
        self.connect(self.get_thread, QtCore.SIGNAL("update_status_bar()"), self.update_status_bar)

    def return_main(self):
        """
        Function is excecuted, when close button is clicked.
        """
        print("return main")
        self.get_thread.terminate()
        self.close()

    def start(self):
        """
        Starts the thread, which means that the run method of the thread is started.
        """
        self.start_button.setEnabled(False)
        self.get_thread.start()

    def stop(self):
        print("Stop programm.")
        self.start_button.setEnabled(True)
        self.get_thread.quit()

    def end(self):
        QtGui.QMessageBox.information(self, "Done!", "Programm finished")



    def closeEvent(self, event):
        """
        This method is called, when the window is closed and will send a signal to the main window to activaete the
        window again.
        :param event:
        """
        self.finished.emit(True)
        # close window
        event.accept()

在以下类中是线程的代码:

class SomeThread(QtCore.QThread):
    finished = QtCore.pyqtSignal(bool)

    def __init__(self):
        QtCore.QThread.__init__(self)

    def __del__(self):
        print("del")
        self.wait()

    def run(self):
        self.prog = long_running_prog(self.emit)  # Sending from the prog signals
        self.prog.run()
        self.prog.closeSystem()  # Leaving the programm in a safe way.

因此,如果按下停止按钮,则该程序应立即以保存方式关闭。 有没有办法以保存的方式中止课程? 例如,当有人按下停止按钮时,我可以将变量传递给long_running_prog类,该类变为True吗? 如果有这种可能,请告诉我如何做?

谢谢您的帮助

希望你能理解我的问题。 问候头晕

除非prog.run(self)会定期检查标志的值以脱离其循环,否则这是不可能的。 一旦实现它,线程上的__del__(self)应该设置该标志,然后再wait

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM