diff --git a/QThread/QuitThread.py b/QThread/QuitThread.py new file mode 100644 index 0000000..5b71a72 --- /dev/null +++ b/QThread/QuitThread.py @@ -0,0 +1,43 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +""" +Created on 2020/11/27 +@author: Irony +@site: https://pyqt.site https://github.com/PyQt5 +@email: 892768447@qq.com +@file: QuitThread +@description: +""" + +__Author__ = 'Irony' +__Copyright__ = 'Copyright (c) 2020' +__Version__ = 'Version 1.0' + +import sys +from time import time + +from PyQt5.QtCore import QThread, QCoreApplication, QTimer + + +class Thread(QThread): + + def run(self): + print('thread id', int(QThread.currentThreadId())) + i = 0 + while i < 101 and not self.isInterruptionRequested(): + print('value', i, time()) + i += 1 + QThread.msleep(500) + print('thread quit') + + +if __name__ == '__main__': + app = QCoreApplication(sys.argv) + t = Thread() + t.finished.connect(app.quit) + t.start() + # 3秒后退出 + print('will quit 3s latter') + QTimer.singleShot(3000, t.requestInterruption) + sys.exit(app.exec_()) diff --git a/QThread/README.md b/QThread/README.md index ef1ee88..b9661c2 100644 --- a/QThread/README.md +++ b/QThread/README.md @@ -5,6 +5,7 @@ - [moveToThread](#2moveToThread) - [线程挂起恢复](#3线程挂起恢复) - [线程休眠唤醒](#4线程休眠唤醒) + - [线程退出](#5线程退出) ## 1、继承QThread [运行 InheritQThread.py](InheritQThread.py) @@ -34,4 +35,11 @@ 使用 `QWaitCondition` 的 `wait` 和 `wakeAll` 方法 -![WakeupThread](ScreenShot/WakeupThread.gif) \ No newline at end of file +![WakeupThread](ScreenShot/WakeupThread.gif) + +## 5、线程退出 +[运行 QuitThread.py](QuitThread.py) + +`isInterruptionRequested` 和 `requestInterruption` 函数作为退出标识调用 + +![QuitThread](ScreenShot/QuitThread.jpg) \ No newline at end of file diff --git a/QThread/ScreenShot/QuitThread.jpg b/QThread/ScreenShot/QuitThread.jpg new file mode 100644 index 0000000..2c96dee Binary files /dev/null and b/QThread/ScreenShot/QuitThread.jpg differ diff --git a/README.md b/README.md index 25d438e..980ef2f 100644 --- a/README.md +++ b/README.md @@ -148,6 +148,7 @@ https://pyqt.site 论坛是专门针对PyQt5学习和提升开设的网站,分 - [moveToThread](QThread/moveToThread.py) - [线程挂起恢复](QThread/SuspendThread.py) - [线程休眠唤醒](QThread/WakeupThread.py) + - [线程退出](QThread/QuitThread.py) - [QtQuick](QtQuick) - [Flat样式](QtQuick/FlatStyle.py)