AsyncCallback 异步回调委托
AsyncCallback
意义:
异步操作完成时调用的方法
语法1:
构造异步回调对象
AsyncCallback 异步回调对象名asyncCallback = new AsyncCallback(异步操作完成时调用的方法MyAsyncCallback);
语法2:
定义委托,并进行异步调用,异步调用完成后自动触发
委托类型Action fn委托名 = Run委托定义;
委托名fn.BeginInvoke(异步回调对象名asyncCallback );
示例:
using System; namespace Examples.AdvancedProgramming.AsynchronousOperations { public class UseDelegateForAsyncCallback { delegate string MethodDelegate(int iCallTime); public static void Main() { MethodDelegate dlgt = (m)=> { return "你输入的数字是"+m; }; AsyncCallback asyncCallback = new AsyncCallback(MyAsyncCallback); Action fn = Run; fn.BeginInvoke(asyncCallback,null); } private static void MyAsyncCallback(IAsyncResult ar) { Console.WriteLine("异步调用"); Console.ReadLine(); } public static void Run() { } } } /// 控制台输出结果: /// 你输入的数字是:5 /// 异步调用