#! D:/soft/Python/Python37 # -*- coding: utf-8 -*- import sys import os from PyQt5.QtCore import (QObject, QThread, QMutex, QMutexLocker, pyqtSignal, QMetaObject, QTime) from PyQt5.QtWidgets import QApplication from PyQt5.QtGui import QGuiApplication """模块路径加到当前模块扫描的路径里""" sys.path.append(os.path.dirname(__file__)) # import numpy as np # import cv2 import copy """ @项目 :ThreadLoop @文件 :myThread.py @作者 :xsh @时间 :2021/11/21 12:59 @功能 : @说明 : """ def DrawInfo(info: object, index: object): print("时间:{0}当前线程ID{1:04}启动\n".format(info, index)) class myThread(QThread): """暂停和结束线程标志""" PauseFlag: bool PauseMutex: QMutex StopFlag: bool StopFlagMutex: QMutex """线程运行的回调函数和参数""" CallBack: object """线程返回值和返回值的回调函数""" Result: object ResultCallBack: object """当前线程序号""" CurrentID: int def __init__(self, ThreadID=int(0), parent=None): super(myThread, self).__init__(parent) self.PauseFlag = False self.PauseMutex = QMutex() self.StopFlag = False self.StopFlagMutex = QMutex() self.CurrentID = ThreadID self.CallBack = [QTime.currentTime().toString, ['HH:MM:SS:sss']] self.Result = None self.ResultCallBack = DrawInfo self.start() def Stop(self, value=True): Locker = QMutexLocker(self.StopFlagMutex) self.StopFlag = value def getStop(self) -> bool: return self.StopFlag def Pause(self, value=True): Locker = QMutexLocker(self.PauseMutex) self.PauseFlag = value def getPause(self) -> bool: return self.PauseFlag def Start(self, *args): if len(args) < 2: return if not self.getStop(): while not self.getPause(): self.msleep(1) else: self.Stop(False) self.Pause(True) self.start() while not self.isRunning(): pass if len(args) == 3: self.CallBack = args[:2] self.ResultCallBack = args[2] else: self.CallBack = args[::] self.ResultCallBack = None self.Pause(False) def run(self) -> None: while not self.getStop(): if self.getPause(): self.msleep(1) continue self.Result = self.CallBack[0](*self.CallBack[1]) if self.ResultCallBack is not None: self.ResultCallBack(self.Result, self.CurrentID) self.ResultCallBack = None self.Pause() pass