I have an application that subscribes to an intensive data feed, constantly receiving messages, and then routes those messages to various "listeners." My first attempt was to just create an Event for the messages, and listeners would simply subscribe to the event in standard C# syntax:
Expand|Select|Wrap|Line Numbers
- EventName += new EventHandler(handlerFunction);
I next tried using a delegate in my event handler:
Expand|Select|Wrap|Line Numbers
- void handlerFunction(object sender, EventArgs e)
- {
- ThreadStart threadMethod = delegate
- {
- PerformTimeIntensiveProcedure();
- };
- Thread thread = new Thread(threadMethod);
- thread.Start();
- }
After some searching on the internet, I came across EventsHelper -- it's actually referenced a lot in these forums. I thought I could use EventsHelper.FireAsync() to get around my problem, however what I'm seeing is there is no way to ensure that the events are received/processed in order. For instance, if each event contains a letter of the alphabet, sequentially, and my "listener" -- event handler -- prints out the letter in the event, it could possibly output: 'A', 'B', 'D', 'C', 'F', ...
I'm wondering what advice you have for handling this situation. I need my main thread to be able to hand off events with minimal time delay (even if the listeners take a while to process the event). I'm thinking each listener is going to need to maintain a queue of incoming events, and asynchronously add incoming events (from the main thread) to each listener's queue, however I'm worried that with FireAsync, the items won't be placed in order in each of the listeners' queues. Any help is greatly appreciated!
RBL