From ce180b071f16d837b0a543ac08a3c6609228e0ed Mon Sep 17 00:00:00 2001 From: Irony <892768447@qq.com> Date: Wed, 14 Mar 2018 16:21:22 +0800 Subject: [PATCH] TerminateThread,dangerous --- 多线程使用/线程挂起恢复.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/多线程使用/线程挂起恢复.py b/多线程使用/线程挂起恢复.py index b2f6636..6deb8c7 100644 --- a/多线程使用/线程挂起恢复.py +++ b/多线程使用/线程挂起恢复.py @@ -53,6 +53,9 @@ class Window(QWidget): self.resumeButton = QPushButton( '恢复线程', self, clicked=self.onResumeThread, enabled=False) layout.addWidget(self.resumeButton) + self.stopButton = QPushButton( + '终止线程', self, clicked=self.onStopThread, enabled=False) + layout.addWidget(self.stopButton) # 当前线程id print('main id', int(QThread.currentThreadId())) @@ -67,6 +70,7 @@ class Window(QWidget): self._thread.start() # 启动线程 self.startButton.setEnabled(False) self.suspendButton.setEnabled(True) + self.stopButton.setEnabled(True) def onSuspendThread(self): if self._thread.handle == -1: @@ -84,6 +88,15 @@ class Window(QWidget): self.suspendButton.setEnabled(True) self.resumeButton.setEnabled(False) + def onStopThread(self): + self.startButton.setEnabled(False) + self.suspendButton.setEnabled(False) + self.resumeButton.setEnabled(False) + ret = ctypes.windll.kernel32.TerminateThread( # @UndefinedVariable + self._thread.handle, 0) + print('终止线程', self._thread.handle, ret) + self.stopButton.setEnabled(False) + def closeEvent(self, event): if self._thread.isRunning(): self._thread.quit()