标签:异步 Console int System static delegate WriteLine result
using System; using System.Runtime.Remoting.Messaging; using System.Threading; using System.Threading.Tasks; namespace LCS.WeiXin.Transfer { class Program { public delegate int AddHandler(int a, int b, int c); static void Main() { Func<int, int, int, int> fun = AddClass.Add; // fun.BeginInvoke(1, 2, 3, CallbackFunc, null); Console.WriteLine("===== 异步回调 AsyncInvokeTest ====="); AddHandler handler = new AddHandler(AddClass.Add); //异步操作接口(注意BeginInvoke方法的不同!) IAsyncResult result = handler.BeginInvoke(1, 2, 3, new AsyncCallback(CallbackFunc), "AsycState:OK"); Console.WriteLine("------继续做别的事情。。。--------"); Console.ReadKey(); } public static void CallbackFunc(IAsyncResult result) { //result 是“加法类.Add()方法”的返回值 //AsyncResult 是IAsyncResult接口的一个实现类,引用空间:System.Runtime.Remoting.Messaging //AsyncDelegate 属性可以强制转换为用户定义的委托的实际类。 Func<int, int, int, int> fun = AddClass.Add; AddHandler handler = (AddHandler)((AsyncResult)result).AsyncDelegate; // fun.EndInvoke(result); Console.WriteLine(handler.EndInvoke(result)); Console.WriteLine(result.AsyncState); } public class AddClass { public static int Add(int a, int b, int c) { Console.WriteLine("\n开始计算:" + a + "+" + b + "+" + c); Thread.Sleep(3000); //模拟该方法运行三秒 Console.WriteLine("计算完成!"); return a + b + c; } } static async void Async2() { await Task.Run(() => { Thread.Sleep(5000); Console.WriteLine("bbb"); }); Console.WriteLine("ccc"); } public static Task<int> ForTest() { return Task.Factory.StartNew<int>(() => { Console.WriteLine("ddd"); return 1; }); } } }
标签:异步,Console,int,System,static,delegate,WriteLine,result 来源: https://www.cnblogs.com/zxiong/p/10594108.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。