Search code examples
pythonpython-3.xpyqtpyqt5qthread

How to kill a running thread


I have a dialog that show the progress of a running thread:

from PyQt5.QtWidgets import *
from PyQt5.uic import loadUi
from PyQt5.QtCore import *

class LoaderProgress(QDialog):
    def __init__(self, parent=None):
        super(LoaderProgress, self).__init__(parent)
        loadUi("CliReportsUI/loaderprogress.ui", self)
        self.pbLoader.setValue(0)
        self.btn_cancel.clicked.connect(self.killthread)

    def watchthread(self,worker):
        self.thread = worker(self)
        self.thread.totsignal.connect(self.pbLoader.setMaximum)
        self.thread.cntsignal.connect(self.updateprogress)
        self.thread.finished.connect(self.close)

    def settitle(self,title):
        self.setWindowTitle(title)

    def startthread(self):
        self.thread.start()

    def updateprogress(self,n):
        self.pbLoader.setValue(n)

    def killthread(self):
        print('How do I do this')

Method in another class runs the thread in the following manner:

    dlg = LoaderProgress(self)
    dlg.watchthread(FileLoader)
    dlg.settitle("Loading The Master File...")
    dlg.show()
    dlg.startthread()

Then there is the worker class:

class FileLoader(QThread):
    totsignal = pyqtSignal(int)
    cntsignal = pyqtSignal(int)

    def __init__(self,parent=None):
        super(FileLoader, self).__init__(parent)
        self.threadactive = True
        self.total = 100

    def run(self):
        self.totsignal.emit(self.total)
        i = 1
        while(i < self.total and self.threadactive):
            print(time.time)
            if(time.time() % 1==0):
                i+=1
                self.cntsignal.emit(i)

On the loader dialog I have a button that will cancel the process (btn_cancel) but I do not know how to kill the running thread if the cancel button is clicked. Thanks for any assistance.


Solution

  • You have to implement a stop() method that changes the threadactive flag to False and waits for the term with wait()

    class FileLoader(QThread):
        totsignal = pyqtSignal(int)
        cntsignal = pyqtSignal(int)
    
        def __init__(self,parent=None):
            super(FileLoader, self).__init__(parent)
            self.threadactive = True
            self.total = 100
    
        def run(self):
            self.totsignal.emit(self.total)
            i = 1
            while(i < self.total and self.threadactive):
                print(time.time())
                if(time.time() % 1==0):
                    i+=1
                    self.cntsignal.emit(i)
    
        def stop(self):
            self.threadactive = False
            self.wait()
    

    And then you call it in killthread method:

    def killthread(self):
        self.thread.stop()
        print('How do I do this')