IAsyncResult Asynchronous design pattern implements asynchronous invocation of the original synchronization method through two methods named Beginoperationname and Endoperationname, such as the FileStream class provides BeginRead and EndRead methods To read bytes asynchronously from a file, which is an asynchronous version of the Read method
The Begin method contains any parameters in the synchronous method signature, plus two additional parameters: a AsyncCallback delegate and a user-defined state object. The delegate is used to invoke the callback method, which is used to pass state information to the callback method. The method returns an object that implements the IAsyncResult interface
The end method ends the asynchronous operation and returns the result, so it contains the ref and out parameters in the synchronous method signature, and the return value type is the same as the synchronization method. The method also includes a IAsyncResult parameter that gets the information about whether the asynchronous operation is complete, and of course the object instance returned by the corresponding Begin method must be passed in when it is used.
If you want to block an application after starting an asynchronous operation, you can call the End method directly, which prevents the application from continuing until the asynchronous operation completes. You can also use IAsyncResult's AsyncWaitHandle property to block threads by calling methods such as WaitOne in them. The difference between the two methods is small, but the former must wait while the latter can set the wait timeout
If you do not block the application, you can determine whether the operation is complete by using the iscompleted state of the IAsyncResult, or use the AsyncCallback delegate to end the asynchronous operation. The AsyncCallback delegate contains a IAsyncResult signature, and the callback method internally calls the End method to get the result of the operation execution
C # Asynchronous Programming mode IAsyncResult