如何知道 QThread 是正确退出还是被终止?
How to get known if QThread exited properly or was terminated?
我发现在 Qt 4.8 中有终止信号,您可以在此处看到:
http://doc.qt.io/archives/qt-4.8/qthread.html#terminated
但是现在Qt 5.8中没有这样的东西了。 http://doc.qt.io/archives/qt-5.8/qthread.html
如果线程完成,即使线程终止,似乎也会发出完成信号。但是是否有选项可以知道 QThread 是正确退出还是被终止?
我使用了@Scheff 提出的以下模式
class MyQThread(QThread):
def __init__(self):
self.__terminated = None
super().__init__()
def wasTerminated(self):
if self.__terminated is None:
return True
return False
def run(self):
# Add at end of run
self.__terminated = False
并在来电中跟随:
class Worker(QObject):
def __init__(self):
super().__init__()
self.thread = MyQThread()
self.thread.finished.connect(self.finishedSlot)
def finishedSlot(self):
if self.thread.wasTerminated():
print("Thread was killed before finished")
else:
print("Thread results are ok")
我发现在 Qt 4.8 中有终止信号,您可以在此处看到: http://doc.qt.io/archives/qt-4.8/qthread.html#terminated
但是现在Qt 5.8中没有这样的东西了。 http://doc.qt.io/archives/qt-5.8/qthread.html
如果线程完成,即使线程终止,似乎也会发出完成信号。但是是否有选项可以知道 QThread 是正确退出还是被终止?
我使用了@Scheff 提出的以下模式
class MyQThread(QThread):
def __init__(self):
self.__terminated = None
super().__init__()
def wasTerminated(self):
if self.__terminated is None:
return True
return False
def run(self):
# Add at end of run
self.__terminated = False
并在来电中跟随:
class Worker(QObject):
def __init__(self):
super().__init__()
self.thread = MyQThread()
self.thread.finished.connect(self.finishedSlot)
def finishedSlot(self):
if self.thread.wasTerminated():
print("Thread was killed before finished")
else:
print("Thread results are ok")