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?
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.
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.
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.
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.
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(...);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With