从c ++ / cli dll中提取c#中的事件 [英] Raise an event in c# from c++/cli dll

查看:121
本文介绍了从c ++ / cli dll中提取c#中的事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

0down votefavorite











在我的c#app中,我有一个进度条。 C#app正在调用一个处理大量文件的c ++ / cli dll。当在dll中处理每个文件时,我想跟踪它在c#app中的进度。为此,我需要在c ++ / cli中引发一个事件并在C#中使用它。从MSDN我收集我在c ++ / cli类中需要以下内容:

  delegate   void  Del( int ); 
event Del ^ E;
void fire( int i){
E(i);
}





http://msdn.microsoft.com/en-us/library/vstudio/58cwt3zh.aspx [ ^ ]

在我的c#app中我有



 MW.Class1 oM =  new  MW.Class1(); 
oM.E + = ProgressBarChanged;



在MSDN中,事件的接收者在同一个c ++ / cli项目中,并显示如何引发事件。在我的项目中,c#app将接收在处理每个文件后应在c ++ / cli中引发的事件。我也搜索了很多,但还没弄清楚。所以,我的问题是,如何在c ++ / cli中引发此事件?一些代码显示了这样做的方式将非常感激。谢谢!

解决方案

在语言之间调用事件没有问题:实际上,没有障碍。问题是不同的:除了声明类型之外,不可能从任何地方调用任何事件实例。您甚至无法派生类并在派生类中执行它。与委托实例相比,这是事件实例的一个重要限制。将其视为防呆功能,没有实际限制:这实际上从未需要,否则可能会导致滥用。



现在,在你的情况下:例如,如果你声明你的 fire 方法并声明类型 public ,你将能够从另一个程序集中激活您的事件 E 。就这么简单。



让我告诉你,你没有使用推荐的模式用于委托类型用于事件。它应该在 System.EvenArgs 上使用(或者更常见的是,从这个类派生的自定义类,让我们调用这个类 MyEventArgs )和委托类型 System.EventHandler (如果是 System.EvenArgs )或 System.EventHandler< MyEventArgs> 。特别是,FxCop(阅读并使用它,这是一个有用的东西)会给你适当的警告。



-SA

0down votefavorite





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);
  }



http://msdn.microsoft.com/en-us/library/vstudio/58cwt3zh.aspx[^]
In my c# app I have

MW.Class1 oM = new MW.Class1();
           oM.E += ProgressBarChanged;


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, ? Some code that shows the way to do it would be greatly appreciated. thanks!

解决方案

There is no a problem to invoke event between languages: practically, there is no a barrier. The problem is different: it's not possible to invoke any event instance from anywhere except its declaring type. You cannot even derive a class and do it in the derived class. This is one important limitation of the event instanced, compared with delegate instances. Think of it as the fool-proof feature, no real limitation: this is never actually needed and could otherwise lead to the possibility of abuse.

Now, in you case: if, for example, you declare your fire method and declaring type public, you will be able to fire your event E from another assembly. As simple as that.

Let me tell you that you are not using recommended pattern for delegate types to be used for events. It should be used on System.EvenArgs (or, more usually, the custom class derived from this class, let's call this class MyEventArgs) and the delegate type System.EventHandler (in case of System.EvenArgs) or System.EventHandler<MyEventArgs>. In particular, FxCop (read about it and use it, this is a useful thing) will give you appropriate warning.

—SA


这篇关于从c ++ / cli dll中提取c#中的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆