c# - event priority and process order -


is there way specify order or priority handle registered event delegates? example, have event processed before other events, want other objects allowed register listeners event well. how can accomplished?

lets want proc1 run before proc 2.

class messageprocessor {     private dataclient client;     public messageprocesser(dataclient dc)     {          client = dc;          client.messagereceived += processmessage;     }      void proc1(messageeventargs e)     {         // process message     }  }  class dataclient {     public event messagereceievedhandler messagereceived; }  void main() {     dataclient dc = new dataclient();     messageprocessor syncprocessor = new messageprocessor(dc); // 1 high priority , needs process sync when arrive before data messages     messageprocessor dataprocessor= new messageprocessor(dc);  // 1 can process data has time when sync messages not being processed.      // other stuff  } 

the reason doing this, have server sending messages on udp stream. send sync messages before burst of data. realize both handlers fire when sync message received, decrease latency syncprocessor objects events processed before dataprocessor events. decrease latency of sync message being processed.

in addition, else on team may want register events process specific messages also. may have own object register event (may not messageprocessor), still sync message should have low latency possible.

edit made objective more clear better example.

when subscribe event multiple times, there no possible way sure of execution order of handlers when event fired.

according this answer, order in subscription order, post author said, it's implementation detail , must not rely on it.

you of course write own "event manager" (see dirty example below) executes handlers in known order, don't think idea. maybe should re-design event remove requirement.

public class myclass {         // anything...     public delegate void myeventhandler(object sender, eventargs e);      public event myeventhandler testevent;      public void test()     {         if (this.testevent != null)         {             this.testevent(this, eventargs.empty);         }     } }  public class eventmanager {     private list<eventhandler> handlers = new list<eventhandler>();      public void addhandler(eventhandler handler)     {         this.handlers.add(handler);     }      public void removehandler(eventhandler handler)     {         this.handlers.remove(handler);     }      public void handler(object sender, eventargs e)     {         foreach (var z in this.handlers)         {             z.invoke(sender, e);         }     } }  private static void main(string[] args) {     myclass test = new myclass();      eventmanager eventmanager = new eventmanager();      // subscribes event     test.testevent += eventmanager.handler;      // adds 2 handlers in known order     eventmanager.addhandler(handler1);     eventmanager.addhandler(handler2);      test.test();      console.readkey(); }  private static void handler1(object sender, eventargs e) {     console.writeline("1"); }  private static void handler2(object sender, eventargs e) {     console.writeline("2"); } 

Comments

Popular posts from this blog

delphi - How to convert bitmaps to video? -

jasper reports - Fixed header in Excel using JasperReports -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -