By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,617 Members | 1,751 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,617 IT Pros & Developers. It's quick & easy.

Efficient way to firing event.....

P: n/a
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

{}

}

-------------------------------------------------------------------------------------
Thanks,
Utkarsh

Dec 21 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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");

Dec 21 '05 #2

P: n/a
Sorry I replied to the wrong post Should be linked to Whats the difference.
Dec 21 '05 #3

P: n/a
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"utkarsh" <ut***********@gmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
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

{}

}

-------------------------------------------------------------------------------------
Thanks,
Utkarsh

Dec 22 '05 #4

P: n/a
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

Dec 22 '05 #5

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

Thanks,
Utkarsh

Dec 22 '05 #6

P: n/a
utkarsh wrote:
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

Dec 22 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.