ADO Events with win32com
Paul Moore
gustav at morpheus.demon.co.uk
Wed Jul 25 16:56:02 EDT 2001
On 25 Jul 2001 12:59:44 -0700, nmack at pacbell.net (angusm) wrote:
>You might want to wait for a more convincing explanation from someone
>else but I was able to duplicate the problem and the code below worked
>for me...it ain't pretty :-).
Thank you, thank you, thank you! It's perfectly clear now. Your code may not be
pretty, but it gave me all the pointers I needed...
>I encountered two problems based on your code:
>1)It appears the event class sees a different 'finished' variable from
>what you define in your module.
Doh! Yes, Python treats any variable which gets assigned to in a function as
local unless it's explicitly declared using 'global'. If I could actually write
code in Python, I'd be dangerous :-)
>2)adAsyncConnect doesn't seem to work because the infinite loop that
>gets created to wait for the events hogs the entire thread. There is
>no way for the dispatched event to break in asynchronously...
Again, I probably should have known this. In order to get COM events, the
application must pump the Windows message loop. Your solution works fine for me,
but there's also a win32gui.PumpWaitingMessages() call which just services any
waiting messages. You can just put a call to this inside the wait loop (which,
of course, would be doing real work if this was a proper application...)
>hope this helps,
>Angus
It does. Thanks for your help. FWIW, the following is a working version of my
code:
from win32com.client import DispatchWithEvents, constants
import win32gui
finished = 0
class ADOEvents:
def OnWillConnect(*args):
pass
def OnConnectComplete(*args):
print `args`
global finished
finished = 1
def Test():
Conn = DispatchWithEvents("ADODB.Connection", ADOEvents)
ConnStr = "Provider=msdaora;Prompt=complete"
Conn.Open(ConnStr, Options=constants.adAsyncConnect)
while not finished:
win32gui.PumpWaitingMessages()
print '...'
pass
if __name__ == '__main__':
Test()
Paul
More information about the Python-list
mailing list