blob: 8c9151cedf4b6f213f524df5fd5585e69dc2348f [file] [log] [blame]
Guido van Rossum2d844d11991-04-07 13:41:50 +00001# Combine a real-time scheduling queue and stdwin event handling.
2# Uses the millisecond timer.
3
4import stdwin
5from stdwinevents import WE_TIMER
6import WindowParent
7import sched
8import time
9
10# Delay function called by the scheduler when it has nothing to do.
11# Return immediately when something is done, or when the delay is up.
12#
13def delayfunc(msecs):
14 #
15 # Check for immediate stdwin event
16 #
17 event = stdwin.pollevent()
18 if event:
19 WindowParent.Dispatch(event)
20 return
21 #
22 # Use millisleep for very short delays or if there are no windows
23 #
24 if msecs < 100 or WindowParent.CountWindows() = 0:
Guido van Rossum001fa6a1991-04-21 19:31:10 +000025 if msecs > 0:
26 time.millisleep(msecs)
Guido van Rossum2d844d11991-04-07 13:41:50 +000027 return
28 #
29 # Post a timer event on an arbitrary window and wait for it
30 #
31 window = WindowParent.AnyWindow()
32 window.settimer(msecs/100)
33 event = stdwin.getevent()
34 window.settimer(0)
35 if event[0] <> WE_TIMER:
36 WindowParent.Dispatch(event)
37
38q = sched.scheduler().init(time.millitimer, delayfunc)
39
40# Export functions enter, enterabs and cancel just like a scheduler
41#
42enter = q.enter
43enterabs = q.enterabs
44cancel = q.cancel
45
46# Emptiness check must check both queues
47#
48def empty():
49 return q.empty() and WindowParent.CountWindows() = 0
50
51# Run until there is nothing left to do
52#
53def run():
54 while not empty():
55 if q.empty():
56 WindowParent.Dispatch(stdwin.getevent())
57 else:
58 q.run()