Microsoft provided a complete set of asynchronous programming design patterns as early as. net2.0, or VS2005. There are three common methods:
1. Use IAsyncResult to call the Asynchronous Method
2. Use Delegation for asynchronous programming
3. asynchronous multi-thread programming using events
Note: The IAsyncResult method is usually implemented by delegation. Therefore, we can consider the first two methods as one.
Aspx "> http://msdn.microsoft.com/zh-cn/library/2e08f6yc.aspx
Use IAsyncResult to call the Asynchronous Method
1. There are two matching methods: BeginXXX () and EndXXX (). They are asynchronous execution of the XXX () method.
The BegingXXX () parameter is added with AsyncCallback and AsyncState Based on the XXX () parameter.
The return value of EndXXX () is the same as that of XXX.
For example
Public delegate string AsyncMethodCaller (object param1, object param2 );
Public class AsyncDemo
{
Private AsyncMethodCaller _ asyncDelegate = new AsyncMethodCaller (Run );
Public IAsyncResult BeginRun (object param1, object param2, AsyncCallback asyncCallBack, Object asyncState)
{
AsyncDelegate. BeginInvoke (param1, param2, callBack, stateObject );
}
Public object EndRun (IAsyncResult asyncResult)
{
M_asyncDelegate.EndInvoke (asyncResult );
}
Public object Run (object param1, object param2 );
} 2. The IAsyncResult object stores information about asynchronous operations.
3. Several methods to prevent application execution
A. Stop asynchronous operations to prevent application execution
When EndXXX () is called, if the IAsyncResult object indicates that the asynchronous operation is not completed, the calling thread will be blocked, that is, the subsequent logic will be blocked.
IAsyncResult result = AsyncDemo. BeginRun (param1, param2, null, null );
AsyncDemo. EndRun (result );
// When Run () method is completed. Continue to go.
ContinueToGo ();
B. Use AsyncWaitHandle to prevent application execution.
Calling the corresponding method in IAsyncResult. AsyncWAitHandle after BeingXXX () can also block the calling thread. For example
IAsyncResult result = AsyncDemo. BeginRun (param1, param2, null, null );
// Wait until the operation completes.
Result. AsyncWaitHandle. WaitOne ();
C. Round-Robin the status of asynchronous operations.
After BeingXXX (), call the IAsyncResult. IsCompleted property to check whether this operation has been completed.
IAsyncResult result = AsyncDemo. BeginRun (param1, param2, null, null );
While (result. IsCompleted! = True)
{
// Waiting until IsCompleted is true.
}
// When Run () method is completed. Continue to go.
ContinueToGo (); d. Use AsyncCallback to end asynchronous operations.
The Endxxx () method is called in the proxy to allow the calling thread to continue running.
Public void MainFunc ()
{
AsyncCallback callBack = new AsyncCallback (ProcessRun );
IAsyncResult result = AsyncDemo. BeginRun (param1, param2, callBack, null );
ContinueToGo ();
}
Public void ProcessRun (IAsyncResult result)
{
AsyncDemo. EndRun (result );
}