Event Priority and Process Order

Can I specify the order or priority for handling registered event delegates? For example, I have an event that I would like to handle immediately before any other events, but I want other objects to also be allowed to register listeners at the event. How can I do that?

Let's say I want proc1 to always 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); // This one is high priority and needs to process all sync immediately when they arrive before any data messages MessageProcessor dataProcessor= new MessageProcessor(dc); // This one can process the data as it has time when sync messages are not being processed. // do other stuff } 

The reason for this is, I have a server that sends messages over a UDP stream. It will send sync messages before bursting data. I understand that both handlers will fire when a synchronization message is received, but to reduce the delay, I would like the events of syncProcessor objects to be processed before the dataProcessor events. This will reduce the delay of the synchronized message being processed.

In addition, someone on my team might want to log events to process certain messages. They may have their own object that will register the event (there may not be a MessageProcessor), even if the Sync message should have as little delay as possible.

EDITED NEXT ACTION.

+4
source share
4 answers

When you subscribe to an event several times, there is no way to verify that your handlers are correctly executed when the event fires.

In accordance with this answer, the order is in a subscription order, but, as the author of the message said, is an implementation detail, and you should not rely on it.

Of course, you can write your own “event manager” (see the example below) that executes your handlers in a known order, but I don't think that would be a good idea. You may need to redesign your event to remove your requirements.

 public class MyClass { // Could be 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 to the event test.TestEvent += eventManager.Handler; // Adds two handlers in a 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"); } 
+5
source

In this case, you better just call the second event at the end of the first event, etc.

+1
source

I'm going to step on a limb and say no. AFAIK events are dispatched ASYNCHRONOUSLY in nature. So they will be sent in the order in which they are connected. however, the results may be returned in a different order. The only way would be to send your second event inside the callback for the first event. It does not depend on the language, but only on the architecture of events.

In your example, since void y calls are not triggered by proc2 at the end of proc1?

0
source

If this is your own event that you defined, the best option would be to extend it to have BeforeMessageReceived, MessageReceived, and AfterMessageReceived events. (well, therefore, perhaps MessageProcessed will be the best root, as you may know before the message is received).

This will give you more control over where the various event handlers occur.

You see the same thing before, events, after patten all over the place.

0
source

Source: https://habr.com/ru/post/1400434/


All Articles