Efficient way to firing event.....

U

utkarsh

Hi All,

I am using the following method "FireAsync" (i got the following
information from the google groups) to fire the event for all the
subscribers.

Is there another way to fire the event to all the subscriber
asynchronously efficiently. As because in my application this method is
being call 60-100 times a second.

Any idea?


---------------------------------------------------------------------

delegate void AsyncInvokeDelegate(Delegate del, params object[] args);

public static void FireAsync(Delegate del, params object[] args)

{

if (del == null)

{

return;

}

Delegate[] delegates = del.GetInvocationList();


AsyncInvokeDelegate invoker = new
AsyncInvokeDelegate(InvokeDelegate);

foreach (Delegate sink in delegates)

{

invoker.BeginInvoke(sink,args,null,null);

}

}


private static void InvokeDelegate(Delegate sink, params object[] args)

{

try

{

sink.DynamicInvoke(args);

}

catch

{}

}
 
B

Ben Dewey

In C# the \ char needs to be repleced with \\ meaning:

XmlNodeList rss = doc.SelectNodes("//item");

needs to be:
XmlNodeList rss = doc.SelectNodes("////item");
or
XmlNodeList rss = doc.SelectNodes(@"//item");
 
N

Nicholas Paldino [.NET/C# MVP]

utkarsh,

How exactly are you defining efficient? Just because you fire your
events on multiple threads doesn't mean that you are going to make it more
efficient.

What is it you are trying to achieve? Depending on the number of
delegates that you have subscribed to your event, combined with the fact
that you are calling this 60-100 times a second, you could very well end up
filling up the thread pool to the point where you are adding delegate calls
faster than you can execute them.

If you want a speed increase, I would recommend defining an interface
and then passing interface implementations to your class for callbacks.
This will be faster than calling a delegate.

Hope this helps.
 
U

utkarsh

Hi Nicholas,

Yes, I want to make it very efficient in terms of the CPU utilization.

My actual problem is as following:-
I am getting the data from the server. It is coming very fast 60-100
times a second.
I each event there are bunch of records are coming. These events are
received on a Static Class, where I fill my class object with data and
add it to collection and then fire an event using the FireAsync()
method as shown in my first post.

There are different 4-5 child form window which further receives these
events. Each window receives the event pickup the relevent data,
add/update/delete in the local arraylist variable. Sort the Data using
the quicksort and then call the refresh() to update the visible rows on
a panel. I am drawing the data myself in tabular form like a grid on a
panel in the OnPaint method.
Since it was very slow to use the datasource and DataGrid or
Infragisitics because updates are coming so faster and these take time
to sort and update on UI.

Now the problem event I have optimized the code at different level as
best i could do, it take more CPU cycle when all the 4-5 windows are
open and so fast processing is going on.

Even If I minimize the 5 window (so no need to paint by window) it
takes 50-80% CPU cycles becuase updates are coming very fast.

But now onwards I am not calling the Refresh() in the event handler of
child window form. I am running a timer in static class which is
notifiing all the 5 window to refresh in every 5 second, this is
helping me much better, but still CPU goes very high to 100% if updates
are more than 90-100 in a second.

So what do you think now in this scenario what are other scope of
improvement here?

Thanks,
Utkarsh
 
U

utkarsh

Actual goal of doing all this is to make the UI more responsive. It
freezes at high CPU usage.

Thanks,
Utkarsh
 
S

Stefan Simek

utkarsh said:
Actual goal of doing all this is to make the UI more responsive. It
freezes at high CPU usage.

Thanks,
Utkarsh

Without going into too much detail, in cases like this, you should try
to process the incoming data in batches instead of breaking it up into
little threadpool workunits. You can't increase the CPU power by using
multiple threads if you have just one core. And there is also the
overhead of GetInvocationList(), BeginInvoke() etc.

HTH,
Stefan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top