引发自定义事件处理程序的问题 [英] Problem raising an custom event handler?

查看:83
本文介绍了引发自定义事件处理程序的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当数据到达缓冲区时,我想提出一个事件。



我尝试了以下内容,但它没有调用事件处理程序方法。





这是代码:



什么我试过了:



Whenever data arrived in the buffer,I want to raise an event.

I tried the following thing,but it is not giving call to the event handler method.


Here is the code:

What I have tried:

public class CurrentDataDataRespParser
   {

       //public Action<byte[]> DataReceived;

       public event EventHandler<byte[]> DataReceived;
       public CurrentDataDataRespParser()
       {

       }

       public int Parse(byte[] pBuffer)
       {
           onDataReceived(pBuffer);
           return pBuffer.Count();
       }

       public virtual void onDataReceived(byte[] pBuffer)
       {
           DataReceivedEventSubscriber subscriber = new DataReceivedEventSubscriber();
           DataReceived?.Invoke(this, pBuffer);
       }

   }










public class DataReceivedEventSubscriber
   {
       public CurrentDataDataRespParser datarespParser;

       public DataReceivedEventSubscriber()
       {
           datarespParser = new CurrentDataDataRespParser();
           datarespParser.DataReceived += DataReceived_PerformOperation;
       }

       public void DataReceived_PerformOperation(Object sender,byte[] pBuffer)
       {
           Console.WriteLine("Event raised,inside event handling method");
       }
   }

推荐答案

解析器有一个事件(DataReceived),没有一个订阅。订阅该事件的唯一事情是DataReceivedEventSubscriber,但它订阅了一个内部自身的CurrentDataDataRespParser实例的事件,因此该事件的唯一发生时间是在该内部调用该对象的方法。订阅者。



您需要更多类似的东西;



The parser has an event (DataReceived) that no-one is subscribed to. The only thing that subscribes to that event is the DataReceivedEventSubscriber, but it subscribes to the event of an instance of CurrentDataDataRespParser that is internal to itself, so the only time that event is going to fire is if the methods are called on that object inside the subscriber.

You need something more like this;

public class CurrentDataDataRespParser
{
    public event EventHandler<byte[]> DataReceived;
    public CurrentDataDataRespParser()
    {

    }

    public int Parse(byte[] pBuffer)
    {
        onDataReceived(pBuffer);
        return pBuffer.Count();
    }

    public virtual void onDataReceived(byte[] pBuffer)
    {
        DataReceived?.Invoke(this, pBuffer);
    }
}

public class DataReceivedEventSubscriber
{
    public void DataReceived_PerformOperation(Object sender, byte[] pBuffer)
    {
        Console.WriteLine("Event raised,inside event handling method");
    }
}





来电代码;





calling code;

CurrentDataDataRespParser client = new CurrentDataDataRespParser();
DataReceivedEventSubscriber sub = new DataReceivedEventSubscriber();

client.DataReceived += sub.DataReceived_PerformOperation;

client.Parse(Encoding.ASCII.GetBytes("Hello"));







这使得订阅者对象中的方法订阅了解析器对象公开的事件。




That makes the method in your subscriber object subscribe to the event exposed by the parser object.


原则上一切看起来都不错。您的订户仍然存在吗?创建订阅者时代码在哪里?



- 此代码当然是无稽之谈:



everything Looks good in principle. so does your subscriber still live? Where is the code when you create the subscriber?

- this code is of course nonsense:

public virtual void onDataReceived(byte[] pBuffer)
{
     DataReceivedEventSubscriber subscriber = new DataReceivedEventSubscriber();
     DataReceived?.Invoke(this, pBuffer);
}







应该只是:






should be just:

public virtual void onDataReceived(byte[] pBuffer)
{
   DataReceived?.Invoke(this, pBuffer);
}


这篇关于引发自定义事件处理程序的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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