Обработчик событий на основе .NET в PythonPython

Программы на Python
Гость
Обработчик событий на основе .NET в Python

Сообщение Гость »


У меня есть устройство с API для программного управления. Это делается с помощью набора файлов .dll на основе .NET.
Пример использования API предоставляется в решении C#, в зависимостях которого есть файлы .dll и такой код (только соответствующая часть). )

Код: Выделить всё

using (InstrumentAccessContainer container = Connection.Get())
{
InstrumentAccess instrument = container.Get(1);

Console.WriteLine(instrument.Name);

EventContainer detector = instrument.GetEventContainer(0);
Console.WriteLine(detector.DetectorClass);

detector.EventArrived += Print_Event;
Thread.CurrentThread.Join(10000);
detector.EventArrived -= Print_Event;
}
}
private void Print_Event(object sender, EventArgs e)
{
using (Ev event = (Ev) e.GetEvent())
{
Console.WriteLine("Event!");
}
}

I am trying to replicate this using Python. Here I can use clr to import the .dll files and use them via pythonnet.
I managed to write this code (relevant part only)

Код: Выделить всё

clr.AddReference("dll1")
clr.AddReference("dll2")

instrument = container.Get(1)
print(instrument.Name)  # this works
detector = instrument.GetEventContainer(0)
print(detector.DetectorClass)  # this works too

# Define a function for MsScanArrived event
def print_event(sender, e):
event = e.GetEvent()
print("Event!")
event.Dispose()

detector.EventArrived += print_event # print("Event!") never happens

def wait_and_detach(event_object, handler, timeout):
def detach():
event_object.EventArrived -= handler

timer = threading.Timer(timeout, detach)
timer.start()
timer.join()  # Wait for the timer to finish

wait_and_detach(detector, detector.EventArrived, 10)
I can successfully print the instrument name, and the detector class, meaning I do get a connection to the device. My problem is in the event handling,

Код: Выделить всё

print_event
is never executed.
I am not sure what to try here, any suggestions? The API does not have much documentation, unfortunately...


Источник: https://stackoverflow.com/questions/781 ... -in-python

Вернуться в «Python»