User Tools

Site Tools


module:python:checkthread:checkthread

Table of Contents

CheckThread

Benutzung

Python-Library Thread.py :

#
import threading as THR
import enum as ENU
#
class EStateThread(ENU.Enum):
    stIdle = 0
    stBusy = 1
    stEnd = 2
#
def CBOnExecute(thread):
    if (None != thread.OnBusy):
        thread.OnBusy(thread)
    if (None != thread.OnEnd):
        thread.OnEnd(thread)
#
class CThread():
    #
    def __init__(self, onstart, onbusy, onabort, onend):
        self.State = EStateThread.stIdle
        self.Thread = THR.Thread(target=CBOnExecute, args=(self,))
        self.OnStart = onstart
        self.OnBusy = onbusy
        self.OnAbort = onabort
        self.OnEnd = onend
    #
    def Start(self):
        self.State = EStateThread.stBusy
        if (None != self.OnStart):
            self.OnStart(self)
        self.Thread.start()
        return   
    #
    def Abort(self):
        self.State = EStateThread.stEnd
        if (None != self.OnAbort):
            self.OnAbort(self)
        return

Aufrufendes Programm CheckThread.py :

#
import time
#
import Thread as THR
#
def CBOnStart(thread):
    print('CBOnStart')
    return
#
def CBOnBusy(thread):
    print('CBOnBusy: 0')
    while (THR.EStateThread.stBusy == thread.State):
        print('CBOnBusy...')
        time.sleep(1.0)
    return
#
def CBOnAbort(thread):
    print('CBOnAbort')
    return
#
def CBOnEnd(thread):
    print('CBOnEnd')
    return
#
#-----------------------------------------------------------
if ('__main__' == __name__):
    print('*** CheckThread: begin')
    #
    Thread = THR.CThread(CBOnStart, CBOnBusy, CBOnAbort, CBOnEnd)
    Thread.Start()
    time.sleep(5.0)
    Thread.Abort()
    #
    print('*** CheckThread: end')
    #
#

Run-Ausgabe im Terminal:

In [19]: runfile('C:/Downloads/python/CheckThread.py', wdir='C:/Downloads/python')
Reloaded modules: Thread
*** CheckThread: begin
CBOnStart
CBOnBusy: 0
CBOnBusy...
CBOnBusy...
CBOnBusy...
CBOnBusy...
CBOnBusy...
CBOnAbort
*** CheckThread: end
CBOnEnd

Version

module/python/checkthread/checkthread.txt · Last modified: 2022/09/13 11:58 by 127.0.0.1