ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

如何从GUI应用程序正确终止QThread?

2019-09-02 21:55:46  阅读:214  来源: 互联网

标签:python multithreading qt pyqt


我尝试在QThread类中使用self.terminate(),并在GUI类中使用self.thread.terminate().我也尝试在两种情况下都使用self.wait().但是,有两种情况发生:

1)线程根本不终止,GUI冻结等待线程完成.线程完成后,GUI解冻,一切恢复正常.

2)线程确实终止,但同时冻结整个应用程序.

我也尝试过使用self.thread.exit().没有快乐.

为了进一步澄清,我试图在GUI中实现一个用户中止按钮,它将在任何时间点终止线程的执行.

提前致谢.

编辑:

这是run()方法:

def run(self):
    if self.create:
        print "calling create f"
        self.emit(SIGNAL("disableCreate(bool)"))
        self.create(self.password, self.email)
        self.stop()            
        self.emit(SIGNAL("finished(bool)"), self.completed)

def stop(self):
     #Tried the following, one by one (and all together too, I was desperate):
     self.terminate()
     self.quit()
     self.exit()
     self.stopped = True
     self.terminated = True
     #Neither works

这是用于中止线程的GUI类的方法:

def on_abort_clicked(self):
     self.thread = threadmodule.Thread()
     #Tried the following, also one by one and altogether:
     self.thread.exit()
     self.thread.wait()
     self.thread.quit()
     self.thread.terminate()
     #Again, none work

解决方法:

从QThread :: terminate的Qt文档:

Warning: This function is dangerous and its use is discouraged. The
thread can be terminated at any point in its code path. Threads can be
terminated while modifying data. There is no chance for the thread to
clean up after itself, unlock any held mutexes, etc. In short, use
this function only if absolutely necessary.

重新思考您的线程策略可能是一个更好的主意,例如你可以使用QThread :: quit()来指示线程干净地退出,而不是试图让线程以这种方式终止.实际上从线程内调用thread.exit()应该这样做,具体取决于你如何实现run().如果您想共享线程运行方法的代码,可能会暗示它为什么不起作用.

标签:python,multithreading,qt,pyqt
来源: https://codeday.me/bug/20190902/1793637.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有