C # asynchronous operations are often used during development. In particular, many IO-related accesses under Windows phone and Silverlight are asynchronous.
Standard Asynchronous methods are usually a pair of methods for in [Invoke] and End [Invoke], so the usage is not much said.
I recently wrote HttpRequest operations that are commonly used in command mode and want to package the commands into asynchronous methods,
For a long time, I used the following method to package Asynchronization:
The first is the regular asynchronous call sequence:
Currently, two classes are used for packaging AsyncCallback, and one for packaging IAsyncResult to package the time series.
The class code is as follows:
/// <Summary>
/// Asynchronous callback proxy
/// </Summary>
Public class AsyncCallbackDelegate
{
/// <Summary>
/// Asynchronous callback that requires proxy
/// </Summary>
Private readonly AsyncCallback asyncCallback;
/// <Summary>
/// Original call object www.2cto.com
/// </Summary>
Public object Delegate {get; set ;}
Public AsyncResultDelegate Result {get; set ;}
/// <Summary>
/// Construct an asynchronous callback proxy
/// </Summary>
/// <Param name = "asyncCallback"> asynchronous callback for proxy </param>
Public AsyncCallbackDelegate (AsyncCallback asyncCallback)
{
This. asyncCallback = asyncCallback;
}
/// <Summary>
/// Asynchronous callback after Packaging
/// </Summary>
/// <Param name = "asyncResult"> </param>
Public void AsyncCallBack (IAsyncResult asyncResult)
{
// Use AsyncResultDelegate to package asyncResult
Var asyncResultDelegate = new AsyncResultDelegate (asyncResult) {Delegate = Delegate };
AsyncCallback (asyncResultDelegate); // call the original asynchronous callback
}
}
/// <Summary>
/// Asynchronous status proxy
/// </Summary>
Public class AsyncResultDelegate: IAsyncResult
{
/// <Summary>
/// Original asynchronous state
/// </Summary>
Private readonly IAsyncResult asyncResult;
/// <Summary>
/// Original call object
/// </Summary>
Public object Delegate {get; set ;}
Public AsyncResultDelegate (IAsyncResult asyncResult)
{
This. asyncResult = asyncResult;
}
# Region decoration mode Packaging
Public object AsyncState
{
Get {return AsyncResult. AsyncState ;}
}
Public System. Threading. WaitHandle AsyncWaitHandle
{
Get {return AsyncResult. AsyncWaitHandle ;}
}
Public bool CompletedSynchronously
{
Get {return AsyncResult. CompletedSynchronously ;}
}
Public bool IsCompleted
{
Get {return AsyncResult. IsCompleted ;}
}
Public IAsyncResult AsyncResult
{
Get {return asyncResult ;}
}
# Endregion
}
Call Sequence After packaging:
The Demo code AddCommand is used to asynchronously execute a + B to return results using proxy packaging.
Public class AddCommand
{
Private readonly int;
Private readonly int B;
Public AddCommand (int a, int B)
{
This. a =;
This. B = B;
}
Public IAsyncResult BeginInvoke (AsyncCallback asyncCallback, object state)
{
Func <int, int, int> addFunc = (x, y) => x + y; // original asynchronous call object used for testing
AsyncCallback callbackDelegate = asyncCallback;
If (asyncCallback! = Null)
{
// Use AsyncCallbackDelegate to package AsyncCallback
Var tmp = new AsyncCallbackDelegate (asyncCallback) {Delegate = addFunc };
CallbackDelegate = tmp. AsyncCallBack;
}
Var asyncResult = addFunc. BeginInvoke (a, B, callbackDelegate, state );
// Use AsyncResultDelegate to package asyncResult
Return new AsyncResultDelegate (asyncResult) {Delegate = addFunc };
}
Public int EndInovke (IAsyncResult result)
{
Var asyncResultDelegate = (AsyncResultDelegate) result; // restore AsyncResultDelegate
Func <int, int, int> addFunc = (Func <int, int, int>) asyncResultDelegate. Delegate; // obtain the original object
Return addFunc. EndInvoke (asyncResultDelegate. AsyncResult); // input the original AsyncResult
}
}
Two call methods are supported to call AddCommand.
Class Program
{
Static void Main (string [] args)
{
// Call EndInovke after the main thread waits
{
AddCommand invoker = new AddCommand (1, 2 );
Var async = invoker. BeginInvoke (null, null );
Async. AsyncWaitHandle. WaitOne ();
Int result = invoker. EndInovke (async );
Console. WriteLine (result );
}
// Call EndInovke by CallBack
{
AddCommand invoker = new AddCommand (5, 9 );
Var async = invoker. BeginInvoke (CallBack, invoker );
Async. AsyncWaitHandle. WaitOne ();
}
Console. ReadLine ();
}
Static void CallBack (IAsyncResult asyncResult)
{
AddCommand invoker = asyncResult. AsyncState as AddCommand;
Int result = invoker. EndInovke (asyncResult );
Console. WriteLine (result );
}
}
From Kiminozo's Tech Blog