Click here to Skip to main content
15,885,767 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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");
       }
   }
Posted
Updated 28-Jun-18 2:32am

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.
 
Share this answer
 
Comments
Nishikant Tayade 28-Jun-18 8:58am    
Thanks!You are right and it is working perfectly,but problem is

client.Parse(Encoding.ASCII.GetBytes("Hello"));//Here in my code I am not directly calling the parse method,there is another method someClass.SendBytes();
which internally goes through many classes and one of the method of that class gives call to Parse method.

When i am debugging again it is showing DataReceived as null.
F-ES Sitecore 28-Jun-18 9:02am    
I just put that code in to test, I had an idea your actual code was different. The concept I think you're missing is that events aren't "global". You don't subscribe to an event property then every instance of that class uses that as an event handler. Events are a per-instance basis of the class. Whatever instance of the class ends up invoking the event handler has to have had the "classInstance += eventHandler" called upon it.

var x1 = new myClass();
x1.MyEvent += myhandler;
x1.DoSomething(); // myHandler is called because x1 has a subscriber
var x2 = new myClass();
x2.DoSomething(); // myhandler is not called because nothing has subscribed to x2
Nishikant Tayade 30-Jun-18 7:00am    
public class Program
{
public delegate int MyOwnDelegate(byte[] pBuffer);

public static void Main(string[] args)
{


CurrentDataDataRespParser client = new CurrentDataDataRespParser();
client.testEvent += new Program.MyOwnDelegate(new Program().TestMethod);

DOF2.sendBytes();
Console.ReadKey();


}

public int TestMethod(byte[] pBuffer)
{
Console.WriteLine("Inside the Test Method");
return 0;
}
}

//CurrentDataDataRespParser is a class where I have declared an event.

public class CurrentDataDataRespParser
{

public event MyOwnDelegate testEvent;
public CurrentDataDataRespParser()
{


}

public int Parse(byte[] pBuffer)
{

testEvent?.Invoke(pBuffer); return pBuffer.Count();
}


}

//and below is the class from where parse method will be called ,which will invoke the event, what I really want is to execute the method TestMethod written in Program class.


public class CurrentDataPacketRespDecoder:PacketDecoder
{
public CurrentDataDataRespParser data_parser_instance;


public CurrentDataPacketRespDecoder()
{
data_parser_instance = new CurrentDataDataRespParser();
}

public override void decode(byte[] pBuffer, byte[] outBuffer
{
if (data_parser_instance == null) { return; }

**here giving call to parse method**
int consumedBytes = data_parser_instance.Parse(pBuffer);


}
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);
}
 
Share this answer
 
Comments
Nishikant Tayade 28-Jun-18 8:13am    
I am creating subscriber in Main method in class Program.

public class Program
{

public static void Main(string[] args)
{





DataReceivedEventSubscriber test = new DataReceivedEventSubscriber ();
DOF2.sendBytes();//this method sends byte array to Parse method in above code which raises event.

Console.ReadKey();

// port.Close();
// port.Dispose();
}


}
}

//The DataReceived is showing null,when I debug the code

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900