module:python:checkthread:checkthread
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
module:python:checkthread:checkthread [2021/12/09 20:44] – omdevelop | module:python:checkthread:checkthread [2022/09/13 11:58] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 8: | Line 8: | ||
] | ] | ||
+ | ===== Benutzung ===== | ||
+ | Python-Library **Thread.py** : | ||
+ | <code python> | ||
+ | # | ||
+ | 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, | ||
+ | self.State = EStateThread.stIdle | ||
+ | self.Thread = THR.Thread(target=CBOnExecute, | ||
+ | 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** : | ||
+ | <code python> | ||
+ | # | ||
+ | import time | ||
+ | # | ||
+ | import Thread as THR | ||
+ | # | ||
+ | def CBOnStart(thread): | ||
+ | print(' | ||
+ | return | ||
+ | # | ||
+ | def CBOnBusy(thread): | ||
+ | print(' | ||
+ | while (THR.EStateThread.stBusy == thread.State): | ||
+ | print(' | ||
+ | time.sleep(1.0) | ||
+ | return | ||
+ | # | ||
+ | def CBOnAbort(thread): | ||
+ | print(' | ||
+ | return | ||
+ | # | ||
+ | def CBOnEnd(thread): | ||
+ | print(' | ||
+ | return | ||
+ | # | ||
+ | # | ||
+ | if (' | ||
+ | print(' | ||
+ | # | ||
+ | Thread = THR.CThread(CBOnStart, | ||
+ | Thread.Start() | ||
+ | time.sleep(5.0) | ||
+ | Thread.Abort() | ||
+ | # | ||
+ | print(' | ||
+ | # | ||
+ | # | ||
+ | </ | ||
+ | |||
+ | Run-Ausgabe im Terminal: | ||
+ | < | ||
+ | In [19]: runfile(' | ||
+ | Reloaded modules: Thread | ||
+ | *** CheckThread: | ||
+ | CBOnStart | ||
+ | CBOnBusy: 0 | ||
+ | CBOnBusy... | ||
+ | CBOnBusy... | ||
+ | CBOnBusy... | ||
+ | CBOnBusy... | ||
+ | CBOnBusy... | ||
+ | CBOnAbort | ||
+ | *** CheckThread: | ||
+ | CBOnEnd | ||
+ | |||
+ | </ | ||
+ | |||
+ | |||
+ | |||
+ | ===== Version ===== | ||
+ | |||
+ | {{: | ||
module/python/checkthread/checkthread.1639079045.txt.gz · Last modified: 2021/12/09 21:44 (external edit)