【发布时间】:2011-12-17 04:50:32
【问题描述】:
我尝试在 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
【问题讨论】:
-
注意:self.terminate === (self.terminated = true),仅此而已。 99% 的实现。
-
谢谢,但这并不能真正帮助解决问题。
-
只有一种方法可以安全地终止线程——让它完成所有的任务。要执行它,您必须在执行硬任务之前检查 thread.terminated。
-
嗯,这当然不是我要找的。span>
标签: python multithreading qt pyqt