线程休眠唤醒

This commit is contained in:
Irony 2018-11-25 22:21:25 +08:00
parent cd4f31ad7f
commit 3513e678d8
5 changed files with 93 additions and 1 deletions

View file

@ -65,6 +65,8 @@ encoding//\u56FE\u8868/PyQtChart/demo/LineChart\u81EA\u5B9A\u4E49xy\u8F74.py=utf
encoding//\u56FE\u8868/PyQtChart/demo/ToolTip.py=utf-8 encoding//\u56FE\u8868/PyQtChart/demo/ToolTip.py=utf-8
encoding//\u56FE\u8868/PyQtChart/demo/ToolTip2.py=utf-8 encoding//\u56FE\u8868/PyQtChart/demo/ToolTip2.py=utf-8
encoding//\u591A\u7EBF\u7A0B/moveToThread.py=utf-8 encoding//\u591A\u7EBF\u7A0B/moveToThread.py=utf-8
encoding//\u591A\u7EBF\u7A0B/\u7EBF\u7A0B\u4F11\u7720\u5524\u9192.py=utf-8
encoding//\u591A\u7EBF\u7A0B/\u7EBF\u7A0B\u6302\u8D77\u6062\u590D.py=utf-8
encoding//\u591A\u7EBF\u7A0B/\u7EE7\u627FQThread.py=utf-8 encoding//\u591A\u7EBF\u7A0B/\u7EE7\u627FQThread.py=utf-8
encoding//\u591A\u9875\u9762/QScrollArea/\u4EFFQQ\u8BBE\u7F6E\u9762\u677F/SettingUi.py=utf-8 encoding//\u591A\u9875\u9762/QScrollArea/\u4EFFQQ\u8BBE\u7F6E\u9762\u677F/SettingUi.py=utf-8
encoding//\u591A\u9875\u9762/QScrollArea/\u4EFFQQ\u8BBE\u7F6E\u9762\u677F/\u4EFFQQ\u8BBE\u7F6E\u9762\u677F.py=utf-8 encoding//\u591A\u9875\u9762/QScrollArea/\u4EFFQQ\u8BBE\u7F6E\u9762\u677F/\u4EFFQQ\u8BBE\u7F6E\u9762\u677F.py=utf-8

View file

@ -92,6 +92,7 @@ https://pyqt5.com 社区是专门针对PyQt5学习和提升开设的博客网站
## [多线程](多线程) ## [多线程](多线程)
1. [线程挂起恢复](多线程/线程挂起恢复.py) 1. [线程挂起恢复](多线程/线程挂起恢复.py)
1. [线程休眠唤醒](多线程/线程休眠唤醒.py)
1. [继承QThread](多线程/继承QThread.py) 1. [继承QThread](多线程/继承QThread.py)
1. [moveToThread](多线程/moveToThread.py) 1. [moveToThread](多线程/moveToThread.py)

View file

@ -17,3 +17,9 @@ PyQt多线程的简单使用例子
`ctypes.windll.kernel32.TerminateThread`终止线程,不推荐 `ctypes.windll.kernel32.TerminateThread`终止线程,不推荐
![截图](ScreenShot/线程挂起恢复.gif) ![截图](ScreenShot/线程挂起恢复.gif)
## [4、线程休眠唤醒](线程休眠唤醒.py)
使用`QWaitCondition`的wait和wakeAll方法
![截图](ScreenShot/线程休眠唤醒.gif)

Binary file not shown.

After

Width:  |  Height:  |  Size: 126 KiB

View file

@ -0,0 +1,83 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Created on 2018年11月11日
@author: Irony
@site: https://pyqt5.com, https://github.com/892768447
@email: 892768447@qq.com
@file:
@description:
"""
from PyQt5.QtCore import QThread, QWaitCondition, QMutex, pyqtSignal
from PyQt5.QtWidgets import QWidget, QVBoxLayout, QPushButton, QProgressBar
__Author__ = """By: Irony
QQ: 892768447
Email: 892768447@qq.com"""
__Copyright__ = 'Copyright (c) 2018 Irony'
__Version__ = 1.0
class Thread(QThread):
valueChange = pyqtSignal(int)
def __init__(self, *args, **kwargs):
super(Thread, self).__init__(*args, **kwargs)
self._isPause = False
self._value = 0
self.cond = QWaitCondition()
self.mutex = QMutex()
def pause(self):
self._isPause = True
def resume(self):
self._isPause = False
self.cond.wakeAll()
def run(self):
while 1:
self.mutex.lock()
if self._isPause:
self.cond.wait(self.mutex)
if self._value > 100:
self._value = 0
self._value += 1
self.valueChange.emit(self._value)
self.msleep(100)
self.mutex.unlock()
class Window(QWidget):
def __init__(self, *args, **kwargs):
super(Window, self).__init__(*args, **kwargs)
layout = QVBoxLayout(self)
self.progressBar = QProgressBar(self)
layout.addWidget(self.progressBar)
layout.addWidget(QPushButton('休眠', self, clicked=self.doWait))
layout.addWidget(QPushButton('唤醒', self, clicked=self.doWake))
self.t = Thread(self)
self.t.valueChange.connect(self.progressBar.setValue)
self.t.start()
def doWait(self):
self.t.pause()
def doWake(self):
self.t.resume()
if __name__ == '__main__':
import sys
import cgitb
sys.excepthook = cgitb.enable(1, None, 5, '')
from PyQt5.QtWidgets import QApplication
app = QApplication(sys.argv)
w = Window()
w.show()
sys.exit(app.exec_())