Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asynchronous Multicast Delegates

I've been doing some work lately on a project that makes extensive use of events. One of the things that I need to do is asynchronously call multiple event handlers on a multicast delegate. I thought the trick would be to call BeginInvoke on each item from GetInvocationList, but it appears as though BeginInvoke doesn't exist there.

Is there a way to do this or do I need to start using ThreadPool.QueueUserWorkItem and sort of roll my own solution that way?

like image 450
Jeremy Privett Avatar asked Sep 21 '09 08:09

Jeremy Privett


People also ask

What are multicast delegates?

The multicast delegate contains a list of the assigned delegates. When the multicast delegate is called, it invokes the delegates in the list, in order. Only delegates of the same type can be combined. The - operator can be used to remove a component delegate from a multicast delegate.

Why do we need multicast delegates?

Multicasting of delegate is an extension of the normal delegate(sometimes termed as Single Cast Delegate). It helps the user to point more than one method in a single call. Properties: Delegates are combined and when you call a delegate then a complete list of methods is called.

What is the difference between events and multicast delegates?

Delegates are pointer to functions and used for call back. Multicast delegates help to invoke multiple callbacks. Events encapsulate delegate and implement publisher and subscriber model.

Which operators are used for multicast delegates?

Important Fact about Multicast Delegate+ or += Operator is used for adding methods to delegates. – or -= Operator is used for removing methods from the delegates list.


1 Answers

GetInvocationList just returns an array of type Delegate which doesn't know the appropriate signature. However, you can cast each returned value to your specific delegate type:

foreach (MyDelegate action in multicast.GetInvocationList())
{
    action.BeginInvoke(...);
}
like image 102
Jon Skeet Avatar answered Oct 07 '22 10:10

Jon Skeet