Hi, we had some discussions about messageloops on this list already. You may remember that you need to run a message loop to receive COM events. Well, this is only true for single threaded apartments, not for multithreaded apartments.
Message loops are strange beasts, especially if you want them to terminate after a certain time, as is the case in test code or in interactive console usage. Finally I found out that the win32 function CoWaitForMultipleHandles is what should be used for this, and I found out how to use that function correctly. Here is the code: """ def pump_messages(timeout): # This following code waits for 'timeout' milliseconds in the way # required for COM, internally doing the correct things depending # on the COM appartment of the current thread. It is possible to # terminate the message loop by pressing CTRL+C, which will raise # a KeyboardInterrupt. hevt = ctypes.windll.kernel32.CreateEventA(None, True, False, None) handles = (ctypes.c_void_p * 1)(hevt) RPC_S_CALLPENDING = -2147417835 @ctypes.WINFUNCTYPE(ctypes.c_int, ctypes.c_uint) def HandlerRoutine(dwCtrlType): if dwCtrlType == 0: # CTRL+C ctypes.windll.kernel32.SetEvent(hevt) return 1 return 0 ctypes.windll.kernel32.SetConsoleCtrlHandler(HandlerRoutine, 1) try: res = ctypes.oledll.ole32.CoWaitForMultipleHandles(0, int(timeout * 1000), len(handles), handles, ctypes.byref(ctypes.c_ulong())) except WindowsError, details: if details[0] != RPC_S_CALLPENDING: # timeout expired raise else: raise KeyboardInterrupt finally: ctypes.windll.kernel32.CloseHandle(hevt) ctypes.windll.kernel32.SetConsoleCtrlHandler(HandlerRoutine, 0) """ I think that this function should be provided by the comtypes.client module; I'm just not tto happy with the name. Any suggestions? Wait(timeout), maybe? Thomas ------------------------------------------------------------------------- This SF.net email is sponsored by: Splunk Inc. Still grepping through log files to find problems? Stop. Now Search log events and configuration files using AJAX and a browser. Download your FREE copy of Splunk now >> http://get.splunk.com/ _______________________________________________ comtypes-users mailing list comtypes-users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/comtypes-users