wait() method of threading.Event() class

Miranda Evans mirandacascade at yahoo.com
Tue May 14 16:06:21 EDT 2002


I must be missing something fundamental that involves notification
between the event firing and the code that polls the results of
MsgWaitForMultipleObjects.  The statements within the "if rc ==
win32event.WAIT_OBJECT_0:" test never get executed even though the
it's pretty clear that the event handler code gets executed.  Using
one of the events from testMSOfficeEvents.py as the basis for this
test.

contents of test_demo.py are:

import time, win32event, win32com.client, pythoncom
class xlEv:
   def __init__(self):
      self.event = win32event.CreateEvent(None, 0, 0, None)
   def OnSheetBeforeDoubleClick(self, sh, target, cancel):
      print 'double click happened'
      win32event.SetEvent(self.event)

def demo():
   g = xlEv()
   xl=win32com.client.DispatchWithEvents("Excel.Application", xlEv)
   xl.Visible=1
   xl.Workbooks.Add()
   start_time = time.time()
   while 1:
      rc = win32event.WaitForMultipleObjects((g.event,),
           0, 1000, win32event.QS_ALLEVENTS)
      if rc == win32event.WAIT_OBJECT_0:
         print 'event signalled'
         break
      elif rc == win32event.WAIT_OBJECT_0+1:
         pythoncom.PumpWaitingMessages()
      # check for timeout
      if time.time() - start_time > 15:
         print 'timed out'
         break

Then in PythonWin
>>> import test_demo
>>> test_demo.demo()

< at this point, I double click in a cell in the Excel app and go back
to PythonWin>

>>> double click happened
>>> timed out

I did some additional tests to keep track of the unique values
assigned to rc during the life of the while loop...the only value that
ever gets assigned to rc is 1.  So, based on these tests, I'm pretty
sure that the "if rc == win32event.WAIT_OBJECT_0" test never evaluates
to true, but I don't know why.

Mark Hammond <mhammond at skippinet.com.au> wrote in message news:<3CE04EC4.5040400 at skippinet.com.au>...
> Miranda Evans wrote:
> > Using win32\Demos\timer_demo.py  as a framework for a solution to this
> > issue led to 2 additional questions:
> > 
> > 1) are the last 2 arguments to MsgWaitForMultipleObjects() in
> > timer_demo.py reversed? (I thought I saw a post that suggested this
> > was the case, but haven't been able to find that post again.)
> 
> That appears to be true.  I have fixed it here.  Good catch ;)
> 
> > 2) given the nature of this particular application (wait for either an
> > event from COM object to fire or for a timer to expire, whichever
> > comes first) is it preferable to use two classes (one for the timer
> > and one for the events from the COM object) or one class (combine
> > timer and COM object event handler in one class)?
> 
> The best is probably to just wait for a single event with a timeout that 
> corresponds to your "timer to expire" interval.
> 
> > 2 class example:
> > # glork is timer
> > class glork
> >    # same __init__ and increments method as in timer_demo.py
> 
> Don't use a timer.  timer_demo was just an example of a MsgWaitFor* loop.
> 
> Something like:
> 
> # evHandle handles events from COM object
> class evHandler
>     def __init__(self):
>        self.event  = win32event.CreateEvent(None, 0, 0, None)
>     def OnSomeEvent(self):
>        win32event.SetEvent(self.event)
> 
> def demo(delay=1000, stop=10):
>     stop_time = time.time() + 60 # 60 seconds in the future.
>     eh=evHandler()
>     while 1:
>        rc = win32event.MsgWaitForMultipleObjects(
>                          (g.event,), # list of objects
>                          0, # wait all
> 			1000,  # timeout
> 			win32event.QS_ALLEVENTS, # type of input
> 			)
>       if rc == win32event.WAIT_OBJECT_0:
>          # Event signalled - the COM object fired!
>          break
>       elif rc == win32event.WAIT_OBJECT_0+1:
>          # Message waiting.
> 	pythoncom.PumpWaitingMessages()
>       else:
>          # This 1 second wait timed-out - see if time has expired.
> 	if time.time()>=stop_time:
>             # time is up!
>             break
> 
> Mark.



More information about the Python-list mailing list