473,326 Members | 1,972 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,326 software developers and data experts.

C# App: Asynchronous Events Help

Greetings,

I believe I have narrowed down my issues to one simple question, and I'm hoping someone with async events experience can help me out.

My question is: how is using EventsHelper.FireAsync() different from using EventsHelper.Fire() BUT having the listener's event handler spawn a new thread to do the work?

In my program, I have a data feed coming in and I have a separate form that has a DataGridView on it. Each time a message comes in (they come in very rapidly) I fire an event and the handler (which exists on the DataGridView's form) adds a row to the DataGridView.

Approach #1: use EventsHelper.FireAsync() to fire the events as they come in. This is nice because the slowness of adding rows to the DataGridView control doesn't slow down the incoming data feed AT ALL -- meaning, if there were other listeners to this event, their performance wouldn't suffer from the slow DataGridView. HOWEVER, this approach doesn't work because of the asynchronous nature, the rows aren't necessarily added in order (message 3 might be processed/added to the DataGridView before messages 1 and 2).

Approach #2: use EventsHelper.Fire() (the synchronous version), and in the event handler, spawn a BackgroundWorker that adds the message to the DataGridView. I don't like this approach because then EVERY handler of this event is going to have to do all this extra work, but I figured it was the only way I could ensure the messages (events) would be received in order. However what I noticed is this approach (spawning the BackgroundWorker thread) doesn't really give a speed improvement over simply doing ALL the work in the same event thread.

Why isn't approach #2, with a BackgroundWorker in the ONLY event handler as fast as approach #1?

I love the speed EventsHelper.FireAsync provides, but I need to maintain order of the events. If anyone has any advice, I'm all ears.

Thanks in advance!

RL
Nov 15 '07 #1
1 2504
To add to my confusion, I add a Thread.Sleep() call in my BackgroundWorker's DoWork() function, and this allows the main (event-generating) thread to get through all of the messages quickly! Why should it matter if there's a Sleep call in the background worker's thread -- if it's on a separate thread, shouldn't the main (event-generating) thread be allowed to continue?
Expand|Select|Wrap|Line Numbers
  1. void bw_DoWork(object sender, DoWorkEventArgs e)
  2. {
  3.     OrderUpdateEventArgs updateEvent = (OrderUpdateEventArgs)e.Argument;
  4.     if (updateEvent.GetEventType() == OrderUpdateEventArgs.OrderUpdateEventType.EVENT_TYPE_FILL) {
  5.         Thread.Sleep(200);  // this speeds up the calling thread!  why!?
  6.         PerformTimeIntensiveTask();
  7.     }
  8. }
  9. void controller_OrderUpdated(object sender, OrderUpdateEventArgs updateEvent)
  10. {
  11.     BackgroundWorker bw = new BackgroundWorker();
  12.     bw.DoWork += new DoWorkEventHandler(bw_DoWork);
  13.     bw.RunWorkerAsync(updateEvent);
  14. }
  15.  
The Sleep call above allows the calling thread to process all of the messages as if there were ZERO delay from this event handler.
Nov 15 '07 #2

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

Similar topics

1
by: MR | last post by:
this is a question on how to design an TCP Listener application. background: There will be several threads that listen on several ports (one per port). when another app requests to connect, the...
1
by: Natalia DeBow | last post by:
Hi, I am working on a Windows-based client-server application. I am involved in the development of the remote client modules. I am using asynchronous delegates to obtain information from...
13
by: ALI-R | last post by:
Hi All, When we say events are processed asynchronously in for instance Sharepoint ,what dose it mean? Thanks for your help. Ali
20
by: Jason Dravet | last post by:
Some time ago I wrote an application that controls the projectors in our classrooms(turn on, turn off, and volume control). This application is written in VB.net 2003. It was working perfectly...
2
by: Gerda | last post by:
Hi! I've implemented many times an asynchronous call of a method with a call backfunction successfully. But to implement this with VB.NET is not so successfully. I can implement all events...
2
by: TrtnJohn | last post by:
I have a multi-threaded class that I am creating that needs needs to raise events when certain asynchronous events occur. I would always like the events to be raised to the primary UI thread of...
5
by: Ryan Liu | last post by:
Hi, I read Microsoft SDK, ms-help://MS.NETFrameworkSDKv1.1/cpguidenf/html/cpovrasynchronousprogramming overview.htm there are 4 ways to call EndInvoke: The code in this topic demonstrates...
1
by: jan.loucka | last post by:
I'm developing WinForms application in .NET 2.0 that talks to web service. The automatically generated proxy (reference class) has got methods for both synchronous and asynchronous invocations of...
1
by: jan.loucka | last post by:
I'm developing WinForms application in .NET 2.0 that talks to web service. The automatically generated proxy (reference class) has got methods for both synchronous and asynchronous invocations of...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.