如何杀死正在运行的线程 [英] How to kill a running thread

查看:60
本文介绍了如何杀死正在运行的线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个对话框显示正在运行的线程的进度:

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

然后是工人阶级:

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)

在加载程序对话框中,我有一个按钮可以取消进程 (btn_cancel),但如果单击取消按钮,我不知道如何终止正在运行的线程.感谢您的帮助.

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.

推荐答案

您必须实现一个 stop() 方法,该方法将 threadactive 标志更改为 False 并用 wait()

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

然后你在 killthread 方法中调用它:

And then you call it in killthread method:

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

这篇关于如何杀死正在运行的线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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