When I call a method asynchronously (using the pattern BeginXxx/EndXxx), I get an IAsyncResult result after calling the BeginXxx. How does the property “isCompleted” (in the returning result variable) get updated if neither the method BeginXxxx or EndXxx have any reference to the result variable?
ex:
// Create the delegate.
AsyncMethodCaller caller = new AsyncMethodCaller(ad.TestMethod);
// Initiate the asychronous call.
IAsyncResult result = caller.BeginInvoke(3000, out threadId, null, null);
// Poll while simulating work.
while(result.IsCompleted == false) {
Thread.Sleep(250);
Console.Write(".");
}
BeginInvokeis returning you theIAsyncResultso it will have a reference to it. This will be internally created and sent back to you.For example,
BeginReadin FileStream createsFileStreamAsyncResultand then returns it: