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.

eventsFreeThreaded.py 3.4KB

1 year ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. # A sample originally provided by Richard Bell, and modified by Mark Hammond.
  2. # This sample demonstrates how to use COM events in a free-threaded world.
  3. # In this world, there is no need to marshall calls across threads, so
  4. # no message loops are needed at all. This means regular cross-thread
  5. # sychronization can be used. In this sample we just wait on win32 event
  6. # objects.
  7. # See also ieEventsApartmentThreaded.py for how to do this in an
  8. # aparment-threaded world, where thread-marshalling complicates things.
  9. # NOTE: This example uses Internet Explorer, but it should not be considerd
  10. # a "best-practices" for writing against IE events, but for working with
  11. # events in general. For example:
  12. # * The first OnDocumentComplete event is not a reliable indicator that the
  13. # URL has completed loading
  14. # * As we are demonstrating the most efficient way of handling events, when
  15. # running this sample you will see an IE Windows briefly appear, but
  16. # vanish without ever being repainted.
  17. import sys
  18. sys.coinit_flags = 0 # specify free threading
  19. import pythoncom
  20. import win32api
  21. import win32com.client
  22. import win32event
  23. # The print statements indicate that COM has actually started another thread
  24. # and will deliver the events to that thread (ie, the events do not actually
  25. # fire on our main thread.
  26. class ExplorerEvents:
  27. def __init__(self):
  28. # We reuse this event for all events.
  29. self.event = win32event.CreateEvent(None, 0, 0, None)
  30. def OnDocumentComplete(self, pDisp=pythoncom.Empty, URL=pythoncom.Empty):
  31. #
  32. # Caution: Since the main thread and events thread(s) are different
  33. # it may be necessary to serialize access to shared data. Because
  34. # this is a simple test case, that is not required here. Your
  35. # situation may be different. Caveat programmer.
  36. #
  37. thread = win32api.GetCurrentThreadId()
  38. print("OnDocumentComplete event processed on thread %d" % thread)
  39. # Set the event our main thread is waiting on.
  40. win32event.SetEvent(self.event)
  41. def OnQuit(self):
  42. thread = win32api.GetCurrentThreadId()
  43. print("OnQuit event processed on thread %d" % thread)
  44. win32event.SetEvent(self.event)
  45. def TestExplorerEvents():
  46. iexplore = win32com.client.DispatchWithEvents(
  47. "InternetExplorer.Application", ExplorerEvents
  48. )
  49. thread = win32api.GetCurrentThreadId()
  50. print("TestExplorerEvents created IE object on thread %d" % thread)
  51. iexplore.Visible = 1
  52. try:
  53. iexplore.Navigate(win32api.GetFullPathName("..\\readme.html"))
  54. except pythoncom.com_error as details:
  55. print("Warning - could not open the test HTML file", details)
  56. # In this free-threaded example, we can simply wait until an event has
  57. # been set - we will give it 2 seconds before giving up.
  58. rc = win32event.WaitForSingleObject(iexplore.event, 2000)
  59. if rc != win32event.WAIT_OBJECT_0:
  60. print("Document load event FAILED to fire!!!")
  61. iexplore.Quit()
  62. # Now we can do the same thing to wait for exit!
  63. # Although Quit generates events, in this free-threaded world we
  64. # do *not* need to run any message pumps.
  65. rc = win32event.WaitForSingleObject(iexplore.event, 2000)
  66. if rc != win32event.WAIT_OBJECT_0:
  67. print("OnQuit event FAILED to fire!!!")
  68. iexplore = None
  69. print("Finished the IE event sample!")
  70. if __name__ == "__main__":
  71. TestExplorerEvents()