blob: 19be2b183308df64d44bbbc9ea6e5d0528bdc77e [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:
25 time.millisleep(msecs)
26 return
27 #
28 # Post a timer event on an arbitrary window and wait for it
29 #
30 window = WindowParent.AnyWindow()
31 window.settimer(msecs/100)
32 event = stdwin.getevent()
33 window.settimer(0)
34 if event[0] <> WE_TIMER:
35 WindowParent.Dispatch(event)
36
37q = sched.scheduler().init(time.millitimer, delayfunc)
38
39# Export functions enter, enterabs and cancel just like a scheduler
40#
41enter = q.enter
42enterabs = q.enterabs
43cancel = q.cancel
44
45# Emptiness check must check both queues
46#
47def empty():
48 return q.empty() and WindowParent.CountWindows() = 0
49
50# Run until there is nothing left to do
51#
52def run():
53 while not empty():
54 if q.empty():
55 WindowParent.Dispatch(stdwin.getevent())
56 else:
57 q.run()