richard richard - 2 years ago 94
C# Question

Call BeginInvoke on MulticastDelegate?

According to Jon Skeet, "You can only call BeginInvoke on a delegate which has a single target invocation."

Why is that? What's the real reason?

Note: For clarification (and because I made this mistake), I am talking about the

on delegates, not on controls.

Answer Source

I think Jon Skeet does a good job explaining in the post you linked:

How do you want the threading to work? Do you have to run each invocation synchronously, but run the whole thing asynchronously with respect to the calling thread, or could you run each invocation asynchronously?

If it's the former, just run a single threadpool work item which calls the delegate synchronously. If it's the latter, get the invocation list with Delegate.GetInvocationList and call BeginInvoke on element of the list in turn.

Basically calling BeginInvoke on a MulticastDelegate is ambiguous, do you want the delegates to wait for each other or not? While in theory it could decide for you, the choice has been made to force you to explicitly select the method you want by calling the delegates in a different fashion.

In other words it is a design choice to avoid confusion. Also it is important to note that BeginInvoke has fallen out of favor and newer methods of asynchronous programming are available making updating this old standard unlikely, so even if they wanted to change now, there is no reason to.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download