android - Raising events in C# from classes -


basically attempting write generic interprocess communication implementation between c# , android platform (using java).

creating socket stack simple enough wondering pattern passing in delegates tcpserver class , raising required events.

the idea far is.

while (connected) {     //compile required data , forth         switch (currentstate)         {                case currentserverstate.eventreceived:                     //raise event...                break;                case currentserverstate.objectreceived:                break;          } } 

so correct implementation expose array of delegates , use reflection match event names or along lines? options? how make sure raised events raised in ui thread given occurring thread created server?

if don't need pass custom arguments event handler can use default eventhandler delegate uses base eventargs.

declare events want make available this:

public event eventhandler eventreceived; public event eventhandler objectreceived; 

declare protected method each event handle raising event you. convenience keep repeating each time need it. inline.

your events null if there no attached event handlers. method handles checking before firing event.

protected void raiseeventreceived(eventargs e) {     if (eventreceived != null)         eventreceived(this, e); } 

then call method when want raise event. method handle checking see if listening event.

public void someothermethod() {     while (isconnected)     {         switch (currentstate)         {             case currentserverstate.eventreceived:                 raiseeventreceived(eventargs.empty);                 break;              case currentserverstate.objectreceived:                 raiseobjectreceived(eventargs.empty);                 break;         }     } } 

you can declare own class pass custom arguments event handlers deriving eventargs such following. convention class should named *something*eventargs.

public class eventreceivedeventargs : eventargs {     // declare properties hold additional event info here } 

then need declare custom delegate takes new event args parameter:

public delegate void eventreceivedeventhandler(object sender, eventreceivedeventargs e); 

update event declarations use new delegate type instead of eventhandler:

public event eventreceivedeventhandler eventreceived; 

and finally, when raising event of course want create new instance of custom event arguments class , intitialize custom properties.

clients can attach multiple handlers events using normal syntax , called on correct thread.


Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -