Re: Efficient way to firing event.....

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



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]
- mvp@xxxxxxxxxxxxxxxxxxxxxxxxxxx

"utkarsh" <utkarshpanwar@xxxxxxxxx> wrote in message
news:1135183377.166355.131920@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
> 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
>


.



Relevant Pages

  • Re: Delegates are useful, and here is why (sample program)
    ... that at the end of the day, delegate is only useful in event handling? ... public class MyCustomClass: EventArgs ... MyCustomClass is a derived class from EventHandler, ... To fire this Event, you use this convention: ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: DragDrop event vs. OnDragDrop(.) - whats the diff?
    ... essence, "hey, I fire event X!"), but it's up to MS to implement it. ... >> The OnDragDrop method is, in essence, a stubbed out event handler ... >> delegate for the DragDrop event. ... It does nothing, unless you override it. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Quick Questions on threading
    ... Well the thread is going to fire off a method to populate ... to have them take up unnecessary resources. ... BeginInvoke on the delegate instance. ... I dont ind ...
    (microsoft.public.dotnet.framework)
  • Re: Starting new threads with arguments
    ... threadpool thread (fire and forget) with strong typing using a generic ... ParameterizedThreadStart) into one generic delegate ... Passed string:[Anonymous method run on a thread pool thread.] ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: C# Events and Delegates - When do they fire?
    ... An event is a delegate method. ... fire, as there is nothing to fire. ... public class MyEventArgs: EventArgs ... So, to consume the event, a client needs to assign a delegate Event Handler: ...
    (microsoft.public.dotnet.languages.csharp)