forked from PyQt5/PyQt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
QuitThread.py
42 lines (34 loc) · 941 Bytes
/
QuitThread.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Created on 2020/11/27
@author: Irony
@site: https://pyqt.site , https://github.com/PyQt5
@email: [email protected]
@file: QuitThread
@description:
"""
import sys
from time import time
try:
from PyQt5.QtCore import QThread, QCoreApplication, QTimer
except ImportError:
from PySide2.QtCore import QThread, QCoreApplication, QTimer
class Thread(QThread):
def run(self):
print('thread id', QThread.currentThread())
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_())