I'm trying to instantiate a .NET object in Python runtime, subscribe to events being fired from an external .NET application via that API object (i.e. the event isn't being fired by Python), and handle the events within python to synchronize the threads.
However, when events fire from external application, an error message is reported: "It is illegal to call while inside message filter".
This is how I've implemented it, which is analogous to how event subscriptions were described in the Python.NET docs.
import clr; clr.AddReference("SomeLibrary.dll")
from SomeLibrary import AnAPIObj
from threading import Semaphore
semaphore = Semaphore(0)
def an_event_handler(sender, args):
semaphore.release()
...
obj = AnAPIObj()
obj.AnEvent += an_event_handler
# waiting for event to be fired and release thread lock
semaphore.acquire()
# output from external application when event fires:
# unable to multicast delegate; it is illegal to call while inside message filter
# python is unresponsive to any event-dependent thread synchronization unless I specify a timeout parameter when trying to acquire the thread lock
Even when I write my own proxy class in c# that subscribes to those events and reflects changes by manipulating properties rather than directly calling a python function, I still receive this error when the object is instantiated from within python.
The only other option that I can think of is writing a daemon application in C# to handle the events and create an API for python to read properties off of.
Does anyone have experience with this type of scenario? Any help would be appreciated.