Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

timer_demo.py 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. # -*- Mode: Python; tab-width: 4 -*-
  2. #
  3. # This module, and the timer.pyd core timer support, were written by
  4. # Sam Rushing (rushing@nightmare.com)
  5. import time
  6. # Timers are based on Windows messages. So we need
  7. # to do the event-loop thing!
  8. import timer
  9. import win32event
  10. import win32gui
  11. # glork holds a simple counter for us.
  12. class glork:
  13. def __init__(self, delay=1000, max=10):
  14. self.x = 0
  15. self.max = max
  16. self.id = timer.set_timer(delay, self.increment)
  17. # Could use the threading module, but this is
  18. # a win32 extension test after all! :-)
  19. self.event = win32event.CreateEvent(None, 0, 0, None)
  20. def increment(self, id, time):
  21. print("x = %d" % self.x)
  22. self.x = self.x + 1
  23. # if we've reached the max count,
  24. # kill off the timer.
  25. if self.x > self.max:
  26. # we could have used 'self.id' here, too
  27. timer.kill_timer(id)
  28. win32event.SetEvent(self.event)
  29. # create a counter that will count from '1' thru '10', incrementing
  30. # once a second, and then stop.
  31. def demo(delay=1000, stop=10):
  32. g = glork(delay, stop)
  33. # Timers are message based - so we need
  34. # To run a message loop while waiting for our timers
  35. # to expire.
  36. start_time = time.time()
  37. while 1:
  38. # We can't simply give a timeout of 30 seconds, as
  39. # we may continouusly be recieving other input messages,
  40. # and therefore never expire.
  41. rc = win32event.MsgWaitForMultipleObjects(
  42. (g.event,), # list of objects
  43. 0, # wait all
  44. 500, # timeout
  45. win32event.QS_ALLEVENTS, # type of input
  46. )
  47. if rc == win32event.WAIT_OBJECT_0:
  48. # Event signalled.
  49. break
  50. elif rc == win32event.WAIT_OBJECT_0 + 1:
  51. # Message waiting.
  52. if win32gui.PumpWaitingMessages():
  53. raise RuntimeError("We got an unexpected WM_QUIT message!")
  54. else:
  55. # This wait timed-out.
  56. if time.time() - start_time > 30:
  57. raise RuntimeError("We timed out waiting for the timers to expire!")
  58. if __name__ == "__main__":
  59. demo()