简体   繁体   English

谁能告诉我我提到的示例是否是回调机制的示例?

[英]Can any one tell me if the example I have mentioned is an example of a callback mechanism

class B
{
     public delegate void CallBack();
     public event CallBacl cllBack;

     void Publish()
     {
       cllBack();
     }
}

class A
{
     B b = new B();
     b.cllBack += new CallBack(test_this)

     public void test_this()
     {

     }
}
  1. Does the above example represent a way of implementing the call back mechanism in .net? 上面的示例是否表示一种在.net中实现回调机制的方法?
  2. Or else do I need to use BeginInvoke and endInvoke for the call back mechanism? 否则我需要使用BeginInvoke和endInvoke作为回调机制吗?
  1. Yes, this is one method. 是的,这是一种方法。 Anyway I'll check this: if (cllBack != null) cllBack(); 无论如何,我都会检查一下: if (cllBack != null) cllBack();
  2. BeginInvoke (look here ) is used for asyncronous operations; BeginInvoke( 在此处查看 )用于异步操作; the method you're using is the syncronous one. 您使用的方法是同步方法。

There are many ways of implementing a callback mechanism, and using delegates is indeed one of them. 实现回调机制的方法有很多,使用委托确实是其中一种。

See this for info on begininvoke etc. also see that for an alternative. 对信息begininvoke等,也看到一种替代。

one caveat that it cost me some pain: 一个警告,它让我有些痛苦:

b.cllBack += new CallBack(test_this)

of course adds a reference to b in a, so even if you do not use b anymore in any other place, b is still referenced and will not be garbage collected. 当然,在a中添加了对b的引用,因此,即使您不再在其他任何地方使用b了,b仍然会被引用,并且不会被垃圾回收。

what i think is a neater approach: 我认为这是一种更整洁的方法:

class A
{
    private delegate B.CallBack myCallBack;
    private B b = new B();

    public A
    {
       myCallBack = new B.CallBack( test_this );
       b.cllBack += myCallBack;
    }


   public void Close()
   {
     c.cllBack -= myCallBack;
   }
}

In short you register when you need the callback, and unregister when you no longer need the callback. 简而言之,您在需要回调时注册,而在不再需要回调时注销。

hth 心连心

Mario 马里奥

PS: be careful, your b is local to the ctor PS:请注意,您的b是ctor的本地对象

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 任何人都可以通过示例给我Dotnetnuke模块本地化 - Can any one give me Dotnetnuke module localization with example 我有这段代码可以在C#中连接UDPClient,请问有人可以吗? - I have this code to connect UDPClient In C#, Please Can Any one Tell me It is fine or not? 如何索引ICollection <T> 就像他们在这个例子中告诉我的那样? - How do I index an ICollection<T> like they tell me to in this example? 谁能告诉我pipeline.com的工作方式吗? - Can any one tell me how the conduit.com works? 任何人都可以建议使用moQ框架的分步示例 - Can any one suggest a step by step example for using moQ framework 有人可以帮助我理解此示例的泛型吗? - Can someone helps me on understanding generics for this example? 任何人都可以告诉我在应用CSS时出错了 - Can any one tell where i went wrong in applying CSS 任何人都能告诉我下面的错误提供者代码之间的差异吗? - Can any one tell me diffrence between below error provider code? 私人int? _City_Id;任何人都可以告诉我“?”在这里意味着什么 - private int? _City_Id; can any one tell me what “?” means here c# 如何在不使用 arrays 的结构中创建长字段(例如 &gt;128 字节)。 我的结构中不能有任何引用类型字段 - c# How do I create a long (>128 bytes for example) field in a structure without using arrays. I can't have any reference type fields in the structure
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM