i am having a bit trouble in understanding it that BeginInvoke() method call in a delegate when asynchronously calls methods, after completion of it’s tasks why can’t BeginInvoke() itself return the return value from the method, why we require EndInvoke() to query BeginInvoke() and return the return value from it.
i know this may be not a good query, but my confusion is real 🙂
Addition: i can call a method asynchronously using DelagateName.BeginInvoke(parameters), now when the matching method in the delegate(suppose it returns a value) finishes it’s work and returns a value, why i need DelegateName.EndInvoke() to get the returned value? why can’t the first call itself can return value on method completion.
When
BeginInvokereturns, the method hasn’t finished – that’s the whole point of it being asynchronous. SoBeginInvokecan only give you a token representing the “promise” of a result at some point – and you use that promise to get the actual result usingEndInvoke, usually in a callback.In .NET 4 this is more pleasantly encapsulated using
TaskandTask<T>of course, but generics weren’t part of .NET 1, hence the somewhat more convoluted approach for delegates.