views:

37

answers:

2

So this question is regarding the .Net IAsyncResult design pattern and the necessity of calling EndInvoke as covered in this question

Background

I have some code where I'm firing off potentially many asynchronous calls to a particular function and then waiting for all these calls to finish before using EndInvoke() to get back all the results.

Question 1

I don't know whether any of the calls has encountered an exception until I call EndInvoke() and in the event that an exception occurs in one of the calls the entire method should fail and the exception gets wrapped into an API specific exception and thrown upwards.

So my first question is what's the best way then to ensure that the remainder of the async calls get properly terminated?

Is a finally block which calls EndInvoke() on the remainder of the unterminated calls (and ignores any further exceptions) the best way to do this?

Question 2

Secondly when I first fire off all my asyc calls I then call WaitHandle.WaitAll() on the array of WaitHandle instances that I've got from my IAsyncResult instances. The method which is firing all these async calls has a timeout to adhere to so I provide this to the WaitAll() method. Then I test whether all the calls have completed, if not then the timeout must have been reached so the method should also fail and throw another API specific exception.

So my second question is what should I do in this case?

I need to call EndInvoke() to terminate all these async calls before I throw the error but at the same time I don't want the code to get stuck since EndInvoke() is blocking. In theory at least if the WaitAll() call times out then all the async calls should themselves have timed out and thrown exceptions (thus completing the call) since they are also governed by a timeout but this timeout is potentially different from the main timeout

+1  A: 

I would iterate through your IAsyncResult objects, wrapping each EndInvoke in a try/catch that stores any generated exception somewhere else. Then, when you've called all the EndInvokes, you can see if you've stored any exceptions and throw an API exception if so. Something like:

var exs = new List<Exception>();

foreach (IAsyncResult iasr in asyncResults) {
    try {
        iasr.EndInvoke();
    }
    catch (Exception e) {
        exs.Add(e);
    }
}

if (exs.Count > 0) {
    throw new MyException(exs.ToReadOnly());
}
thecoop
A: 

I recommend using Tasks instead of IAsyncResult if at all possible. They have nicer "continuation" semantics, and can wrap an IAsyncResult API, calling EndInvoke appropriately for you.

Stephen Cleary
Any links or code examples to go with that?
RobV
See the "Wrapping APM Operations in a Task" section of [this MSDN page](http://msdn.microsoft.com/en-us/library/dd997423.aspx).
Stephen Cleary
Ah, .Net 4.0 stuff is not an option as the API needs to stay 3.5 for the time being since we build for both MS and Mono and Mono's 4.0 implementation is not sufficiently stable to consider making this migration at this time
RobV