Can I use a single instance of a delegate to start multiple Asynchronous Requests?

Posted by RobV on Stack Overflow See other posts from Stack Overflow or by RobV
Published on 2010-06-15T11:22:16Z Indexed on 2010/06/15 12:02 UTC
Read the original article Hit count: 168

Filed under:
|
|

Just wondered if someone could clarify the use of BeginInvoke on an instance of some delegate when you want to make multiple asynchronous calls since the MSDN documentation doesn't really cover/mention this at all.

What I want to do is something like the following:

MyDelegate d = new MyDelegate(this.TargetMethod);
List<IAsyncResult> results = new List<IAsyncResult>();

//Start multiple asynchronous calls
for (int i = 0; i < 4; i++)
{
   results.Add(d.BeginInvoke(someParams, null, null));
}

//Wait for all my calls to finish
WaitHandle.WaitAll(results.Select(r => r.AsyncWaitHandle).ToArray());

//Process the Results

The question is can I do this with one instance of the delegate or do I need an instance of the delegate for each individual call?

Given that EndInvoke() takes an IAsyncResult as a parameter I would assume that the former is correct but I can't see anything in the documentation to indicate either way.

© Stack Overflow or respective owner

Related posts about .NET

Related posts about async