-->

Asynchronous Multicast Delegates

2020-07-02 11:01发布

问题:

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?

回答1:

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(...);
}