[python-win32] DispatchWithEvents design question

Tim Roberts timr at probo.com
Wed Jun 6 18:41:07 CEST 2007


Richard Bell wrote:
> Here is some example code:
>
> --- code ---
> # test events WITHOUT a message pump
> import win32com.client
>
> class events():
>     def OnVisible(self, vis):
>         print 'OnVisible is:', vis
>     def OnBeforeNavigate2(self,pDisp,url,flags,
>                           targetFrameName,postData,
>                           headers,cancel):
>         print "OnBeforeNavigate2 on url [%s] on frame [%s]"% \
>               (url,targetFrameName)
>     def OnCommandStateChange(self,
>                              Command,
>                              Enable):
>         print 'OnCommandStateChange: %s %s'%(Command,Enable)
>
> ie = win32com.client.DispatchWithEvents(
>     'InternetExplorer.Application',
>     events)
> # NO PUMP MESSAGE
> ie.Visible = 1
> ie.Navigate('www.google.com')
> --- end code ---
>
> And here is the output I get
> --- output ---
> OnVisible is: True
> OnCommandStateChange: 2 False
> OnBeforeNavigate2 on url [http://www.google.com/] on frame []
> OnCommandStateChange: 1 False
> --- end output ---
>
> In this example there is no message pump that I'm aware of yet the event
> routines clearly execute.  What's going on?
>   

These are synchronous events.  That is, these events are generated
directly by the requests you made.  I believe you will find that the
"OnVisible" callback occurs before "ie.Visible = 1" returns.  It's being
handled by that same thread, which is suspended waiting for IE to
finish.  A message pump is only required for events that arrive
asynchronously.  It's still single threaded.

If you're familiar with the Windows GUI, this is similar to the
difference between PostMessage and SendMessage (ignoring interprocess
calls for now).  SendMessage calls directly into the window procedure of
the target window without going through the message queue; when it
returns, the message is handled.  PostMessage is fire-and-forget; it
will be processed later, when the message loop gets around to pulling
the message.

-- 
Tim Roberts, timr at probo.com
Providenza & Boekelheide, Inc.



More information about the Python-win32 mailing list