pywin32 icon indicating copy to clipboard operation
pywin32 copied to clipboard

Allow detaching events from a COM client

Open weshouman opened this issue 3 years ago • 2 comments

Type: Feature

Expected behavior

Detach a specific event after attaching it to a COM object.

Actual behavior

Not supported

Steps to reproduce the problem

from win32com.client import WithEvents

# ...

class ClassOfHandlers():
  def OnStart(self):
    print("Start observed")

class AnotherClassOfHandlers():
  def OnStart(self):
    print("Start observed from another")

handler = WithEvents(client, ClassOfHandlers)
# ...
WithEvents(client, AnotherClassOfHandlers)
# ...
# Deregister `ClassOfHandlers`

Notes:

  • There's a workaround of having a list in the ClassOfHandlers that we manipulate, which I described in this StackOverflow answer, but it is of course not ideal.
  • It would be great to cover the scenario when WithEvents() is needed to be executed from inside the thread runnable, to satisfy the constraint for the PumpWaitingMessages() of having WithEvents() called from the same thread, while supporting this event detaching feature.
  • Further details are [found here(https://stackoverflow.com/q/73864764/2730737).
  • IConnectionPoint::Unadvise() may be required to be used in the background based on DS_London's suggestion.

Version of Python and pywin32

Python: 3.10.0 pywin32: 304

weshouman avatar Oct 02 '22 01:10 weshouman

How to have events for Python Active Scripting? I searched everywhere but ended up here. Any hints?

wxinix-2022 avatar Mar 12 '23 04:03 wxinix-2022

@wxinix-2022 I'd suggest opening a support request in Discussions or on the python-win32 mailing list, describing your complete use case and what you already tried.

Avasam avatar Apr 10 '25 04:04 Avasam