【发布时间】:2016-04-29 13:48:07
【问题描述】:
我正在尝试将事件处理程序附加到 COM Object 的事件。但我得到一个 InvalidOperationException。
using S7PROSIMLib;
private S7ProSim ps = new S7ProSim();
ps.Connect();
ps.SetScanMode(ScanModeConstants.SingleScan);
ps.BeginScanNotify();
try {
ps.ScanFinished += Ps_ScanFinished;
//IS7ProSimEvents_ScanFinishedEventHandler scanFinishedDelegate = new IS7ProSimEvents_ScanFinishedEventHandler(Ps_ScanFinished);
//ps.ScanFinished += scanFinishedDelegate;
}
catch (InvalidOperationException ex)
{
Console.WriteLine(ex.Source);
Console.WriteLine(ex.InnerException);
Console.WriteLine(ex.Message);
Console.WriteLine(ex.StackTrace);
}
private void Ps_ScanFinished(object ScanInfo)
{
Console.WriteLine("Event fired");
}
输出是:
event invocation for COM objects requires event to be attributed with DispIdAttribute
at System.Runtime.InteropServices.ComAwareEventInfo.GetDataForComInvocation(EventInfo eventInfo, Guid& sourceIid, Int32& dispid)
at System.Runtime.InteropServices.ComAwareEventInfo.AddEventHandler(Object target, Delegate handler)
at PiPLCSimBridge.Form1.Form1_Load(Object sender, EventArgs e) in C:\PiPLCSimBridge\Form1.cs:Line 72.
我也尝试使用注释掉的代码,但得到了相同的异常。
像这样附加事件应该可以工作,一个使用相同 COM 接口的流行工具 is doing it this way.
我的代码有什么问题?
【问题讨论】:
-
您是否尝试将 RaspberryPi 连接到 300/400 模拟器?
-
是的,你怎么知道?:D
-
:) 听起来很整洁。您是否尝试将 Pi 用作远程 IO 或其他东西?您对项目的帮助感兴趣吗?
-
是的,我打算用它来将模拟扩展到现实世界。一些帮助会很棒,我该如何联系你?