Regarding C#’s asynchronous callback, in ActionScript 3.0, there is the keyword Function, and you can directly pass the class function as the callback function. But in C#, delegates need to be used, and the most commonly used ones are: Action / Func. Of course you can use the keyword delegate to customize the delegate. Here, Action / Func is used as an example to explain C#’s asynchronous callback. If you don’t understand C#’s delegation mechanism, you need to search it on Baidu/Google first, and then read this blog.
BeginInvoke method is used to start an asynchronous call. It has the same parameters as the method you need to execute asynchronously, except that there are two additional parameters: the first: the callback function after the asynchronous call is completed. This function has one parameter: IAsyncResult. The second is the state Object, which can pass any value and use IAsyncResult.AsyncState to receive it.
Things to note are: For an asynchronous calling function with a Return (return value), how to obtain its return value: EndInvoke method, the noteworthy EndInvoke method: can be executed in the callback function after the asynchronous call ends, or in the asynchronous Execute after calling the code: It is just that this time it is executed as a synchronous function (the asynchronous function is executed as a synchronous function). The understanding is that now I want to get the Return result, so I stay here, wait for the result to Return, and then execute it later. , equivalent to a synchronization function.
Okay, end the nonsense, upper text:
1using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace AnycCallBack161005.com { public class Anyc { private Dictionary<Action<int, int>, IAsyncResult> noReturnDic; private Dictionary<Func<int, int, int>, IAsyncResult> hasReturnDic; public Anyc() { this.noReturnDic = new Dictionary<Action<int, int>, IAsyncResult>(); this.hasReturnDic = new Dictionary<Func<int, int, int>, IAsyncResult>(); } public void DoAction(Action<int,int> callBack , int a , int b ) { if(callBack != null) { if(this.noReturnDic.ContainsKey(callBack)) { callBack.EndInvoke(this.noReturnDic[callBack]);//强制执行上回的CallBack } this.noReturnDic[callBack] = callBack.BeginInvoke(a, b, this.DoActionComplete, callBack); } } private void DoActionComplete( IAsyncResult ar ) { Action<int, int> callBack = ar.AsyncState as Action<int, int>; this.noReturnDic.Remove(callBack); } public void DoFunc( Func<int ,int , int> callBack , int a , int b ) { if(callBack != null) { if(this.hasReturnDic.ContainsKey(callBack)) { int re = callBack.EndInvoke(this.hasReturnDic[callBack]); Console.WriteLine("得到的结果为 {0} ", re); } this.hasReturnDic[callBack] = callBack.BeginInvoke(a,b,this.DoFuncComplete,callBack); } } private void DoFuncComplete(IAsyncResult ar) { Func<int, int, int> callBack = ar.AsyncState as Func<int, int, int>; int re = callBack.EndInvoke(ar); Console.WriteLine("得到的结果为* {0} ", re); this.hasReturnDic.Remove(callBack); } } }
//Calling code:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using AnycCallBack161005.com; namespace AnycCallBack161005 { class Program { static void Main(string[] args) { Action<int,int> testActionAnyc = ( a , b ) =>Console.WriteLine("Action得到的结果 : {0}",(a+b).ToString()); Func<int, int,int> testFuncAnyc = (a, b) => a + b; Anyc anyc = new Anyc(); anyc.DoAction(testActionAnyc, 3, 4); anyc.DoAction(testActionAnyc, 3, 5); anyc.DoAction(testActionAnyc, 3, 6); anyc.DoFunc(testFuncAnyc, 7, 8); anyc.DoAction(testActionAnyc, 3, 7); anyc.DoAction(testActionAnyc, 3, 8); Console.Read(); } } }
Result: