python wia and RegisterEvent

News123 news1234 at free.fr
Tue Apr 20 20:06:08 EDT 2010


Hi Roger,

Roger Upole wrote:
> gelonida wrote:
> ...
>> while True:
>>     print "sleep"
>>     time.sleep(10)
>>
>> When I plug / unplug a USB WIA device nothing shows up.
>> My C# implementation prints messages on wiaEventDeviceConnected /
>> wiaEventDeviceDisconnected events if I register them.
>>
>> What am I missing?
> 
> You need to be processing messages to receive COM events.
> Try replacing your sleep loop with pythoncom.PumpMessages().
> 

Thanks a lot

Indeed the only thing missing was a call to pythoncom.PumpMessages()

I fell over another tiny detail though, as I wanted to keep my main
application running ( the place holder was my sleep loop)

Tt seems, that PumpMessages() cannot be in another thread.


So I ended up with:

import win32com.client,pythoncom,time,threading

defaultNamedNotOptArg=pythoncom.Empty
wiaEventDeviceConnected =u'{A28BBADE-64B6-11D2-A231-00C04FA31809}'

class MgrHandlerClass:
    def OnEvent(self, EventID=defaultNamedNotOptArg,
            DeviceID=defaultNamedNotOptArg,
            ItemID=defaultNamedNotOptArg):
        print "Called back with ..."


mgrhndlr = MgrHandlerClass()
manager = win32com.client.DispatchWithEvents("WIA.DeviceManager",
MgrHandlerClass)
manager.RegisterEvent(EventID=wiaEventDeviceConnected,DeviceID=u'*')

def sleeploop():
    while True:
        print "sleep"
        time.sleep(10)

thrd = threading.Thread(target=sleeploop)
thrd.start()
# doesn't work if pump is in another thread
pythoncom.PumpMessages()


bye



N



More information about the Python-list mailing list