Prioritizing event handlers

I have the following code where I process the event twice. However, I always want to ensure that mynewclass always processes the event first, and then the code of the local event handler is run. I understand that the MyClass event should be fired first, since it was fired first, but because the thread is happening and enqueuing, I think it takes too long and does something in myhandleeventlocal before I want it to did it. Anyway, can I wait for this to happen?

  public MyMainClass { private MyMethod() { MyClass mynewclass = new MyClass(); mynewclass.myObject += MyHandler(myhandleventlocal); mynewclass.loadedevent += EventHandler(loadedevent) } private void myhandleventlocal() { //do stuff } private void loadedevent() { //do some stuff } } public MyClass { public MyObject myObject; public event loadedevent; public MyClass() { myObject = new MyObject(); myObject += MyHandler(myhandlevent); } private void myhandlevent(long value, string detail) { //Start a thread //Enqueue value and detail //On seperate thread dequeue value and process it //Raise loadedevent event } } 

UPDATE: I updated my question and code to demonstrate the problem.

+2
source share
4 answers

By default, event handlers are called in the order in which you add them, so if you always add handlers in the order in which you want to run them, it should work.

From an article by John Skeet about events and delegates :

[...] additional delegates are added and removed from the end of the list [...]

Note. You can override the default behavior of events by changing the add and remove operations on your event to specify a different behavior. You can then save the event handlers in a list that you control yourself and process the order of fire based on any rules that you like.

+6
source

If you cannot guarantee the order that event handlers will be added, just add it for mynewclass , and then call another code in this code.

+3
source

Since event handlers are called in the order in which you add them, based on the code that I see in your question, you cannot call the mynewclass handler first. The event handler created by MyClass is always added first.

One solution would be priority management for event handlers. Instead of using the built-in event handler + = / - = operators instead, you should have methods to add and remove events, where you can explicitly specify the order. That way, if the class knows that it needs to handle the event first, it can ask for it. However, be careful because you can easily run into a situation where several classes insist that they handle the event first.

Here is some quick and dirty code to get you started:

 class MyClass { private LinkedList<MyEventHandler> eventHandlers; public enum Ordering { First, Last, ... }; public void AddHandler(MyEventHandler handler, Ordering order) { switch(order) { case Ordering.First: eventHandlers.AddFirst(handler); break; // fill in other cases here... } } public void RaiseEvent() { // call handlers in order foreach(MyEventHandler handler in eventHandlers) eventHandler(); } } 
+2
source

Referring to the siride solution, you can also implement your handlers and solve this position in this way. Like inverting order (always add at the beginning) or add some logic.

0
source

All Articles