提高C ++ / CLI DLL事件,并在C#中消耗 [英] raise events in c++/cli dll and consume in c#
问题描述
在我的C#应用程序我有一个进度条。 C#应用程序中调用C ++ / CLI DLL用于处理大量的文件。当每个文件在DLL处理,我想跟踪它在C#应用程序的进展。为了这个,我需要提高的C ++ / CLI事件,并在C#中使用它。从MSDN我收集我需要在我的C ++ / CLI类以下内容:
委托无效德尔(INT);
事件德尔^ E;
无效火(int i)以{
E(I)
}
的 MSDN活动
在MSDN事件的接收器是在同一个C ++ / CLI的项目,它是展示了如何引发事件。在我的项目,在C#应用程序是接收应在C ++ / CLI每个文件处理后引发的事件。我GOOGLE了很多太多,但一直没能弄明白呢。所以,我的问题是,如何提高C ++ / CLI这个事件,如何将我的C#程序连接到它?一些代码,显示做的方式,将不胜感激。
的感谢!
马特,
感谢响应。
在C#中我试图
MW.Class1 OM =新MW.Class1();
oM.fire + = ProgressBarChanged(INT I);
这并不编译说不能分配,因为火是方法组。我也有我的C#程序,处理程序
公共无效ProgressBarChanged(int i)以
{
}
这确实更好,虽然编译器说INT预计不会,不会编译
oM.fire + =新的EventHandler(ProgressBarChanged(int i)以);
随着错误消息指出,火
是一个方法,而不是一个事件。呼叫 + =
上的事件,并留下关闭()
当你引用事件处理方法。
oM.E + = this.ProgressBarChanged;
至于触发事件,你在做正确的:只需拨打您的活动就像一个委托或一个方法,传递事件的参数。
In my c# app I have a progressbar. C# app is calling a c++/cli dll which processes a number of files. As each file is processed in the dll I would like to track it's progress in the c# app. For this i need to raise an event in c++/cli and consume it in C#. From MSDN I gather I need in my c++/cli class the following:
delegate void Del(int);
event Del^ E;
void fire(int i) {
E(i);
}
In MSDN the receiver of the event is in the same c++/cli project and it is shown how to raise the event. In my project, the c# app is to receive the event which should be raised in c++/cli after each file is processed. I've googled a lot too but have not been able to figure it out yet. So, my question is, how do I raise this event in c++/cli, and how do I connect my c# program to it? Some code that shows the way to do it would be greatly appreciated. thanks!
Matt, thanks for responding. In C# I am trying
MW.Class1 oM = new MW.Class1();
oM.fire += ProgressBarChanged(int i);
It does not compile saying cannot assign because fire is a method group. I also have in my C# program, the handler
public void ProgressBarChanged(int i )
{
}
This does better though compiler says int is not expected and does not compile
oM.fire += new EventHandler(ProgressBarChanged(int i));
As the error message says, fire
is a method, not an event. Call +=
on the event, and leave off the ()
when you reference the event handler method.
oM.E += this.ProgressBarChanged;
As for firing the event, you're doing that correctly: Just call your event like a delegate or a method, passing the event arguments.
这篇关于提高C ++ / CLI DLL事件,并在C#中消耗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!