473,411 Members | 2,059 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

EventsHelper c# .NET -- FireAsync help

Greetings,

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
  1. EventName += new EventHandler(handlerFunction);
This worked well with one limitation: if one of the "listeners" handling the event took any substantial amount of time to process the event, the calling (main) thread's performance would suffer because the event is handled on the same thread as it's triggered.

I next tried using a delegate in my event handler:

Expand|Select|Wrap|Line Numbers
  1. void handlerFunction(object sender, EventArgs e)
  2. {
  3.     ThreadStart threadMethod = delegate
  4.     {
  5.         PerformTimeIntensiveProcedure();
  6.     };
  7.  
  8.     Thread thread = new Thread(threadMethod);
  9.     thread.Start();
  10. }
  11.  
Thinking that this would perform the time-intensive app on a separate thread, and allow the calling thread (the one that triggered the event) to move on, however this also didn't work as I expected.

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
Nov 14 '07 #1
4 5127
Plater
7,872 Expert 4TB
You could try implementing a Queue (there's already a class for it)?
When your application generates an "event" it pushes the relevent data onto the Queue object.
Meanwhile a seperate thread(timer? whatever) is looking at the queue and will pull off the oldest "event data" and process it in the correct order.

Your main thread keeps pushing them on and the other thread keeps taking the oldest one off.


Main (Queue) Other Thread
F -> EDCB -> A
Nov 14 '07 #2
Thanks Plater. Yes, I could implement a producer/consumer model that would have one thread (the main thread) adding to the master queue of events, and have another thread -- the consumer thread -- pulling off the queue and triggering events. however, this approach will still suffer in that if I have 10 listeners all subscribed to the same event, listener10 won't learn of the event until listeners 1-9 process the event (yes, the main/producer thread will continue on just fine adding to the queue, but ultimately I need the listeners to be able to receive events when they happen. Does this make sense? It's almost like I need to broadcast the event to each listener simultaneously, so they can each do their own thing with the event -- I figured the FireAsync would do this for me, however it also introduces the possibility that the events will be received out of order. How can I achieve this broadcast effect, but have each listener receive the events in order?
Nov 14 '07 #3
Plater
7,872 Expert 4TB
Can you get all your listeners running on seperate threads?
Nov 14 '07 #4
Can you get all your listeners running on seperate threads?
Yes, they are all on separate threads. It's almost like I need to be able to designate a separate queue for EACH listener, and then the main thread simply places each event in each listener's queue, that they can handle at their own pace. I'd like to not have to do that explicitly, however, which is why I was hoping there was some mechanism (like FireAsynch) that could accomplish precisely this.
Nov 14 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

21
by: Dave | last post by:
After following Microsofts admonition to reformat my system before doing a final compilation of my app I got many warnings/errors upon compiling an rtf file created in word. I used the Help...
9
by: Tom | last post by:
A question for gui application programmers. . . I 've got some GUI programs, written in Python/wxPython, and I've got a help button and a help menu item. Also, I've got a compiled file made with...
4
by: Sarir Khamsi | last post by:
Is there a way to get help the way you get it from the Python interpreter (eg, 'help(dir)' gives help on the 'dir' command) in the module cmd.Cmd? I know how to add commands and help text to...
2
by: Sudheer Kareem | last post by:
Dear All Please tell me how to assosiate help files with my Vb.net Project. Regards Sudheer
6
by: wukexin | last post by:
Help me, good men. I find mang books that introduce bit "mang header files",they talk too bit,in fact it is my too fool, I don't learn it, I have do a test program, but I have no correct doing...
6
by: d.warnermurray | last post by:
I am doing a project for school that involves creating help files for a html authoring tool. If you could help me with answers to some questions it would really help. 1. What tasks do you expect...
5
by: Craig Keightley | last post by:
Please help, i have attached my page which worksin IE but i cannnot get the drop down menu to fucntion in firefox. Any one have any ideas why? Many Thanks Craig ...
3
by: slikrik98 | last post by:
Greetings, This is a separate question from my earlier EventsHelper.FireAsync question. I am seeing strange behavior from this method, and I'm hoping someone can shed some light onto it. I have...
6
by: =?Utf-8?B?RGFuaWVsIEplZmZyZXk=?= | last post by:
Hello, I am a C# developer and am now starting to do some work in VB.NET and for the life of me cant get something working the way I cant. If anyone can offer any suggestions on how to convert...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.