码迷,mamicode.com
首页 > Windows程序 > 详细

C#委托调用(同步调用,异步调用,异步回调)

时间:2017-08-20 18:29:00      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:nbsp   back   turn   pre   开始   var   return   call   begin   

 

     #region 委托回调
        static void delegateTest()
        {
            Console.WriteLine("同步调用");
            SubDelegate subDel = Sub;
            var result = subDel.Invoke(3, 4);
            Console.WriteLine("继续");
            Console.WriteLine(result);
            Console.ReadKey();

            Console.WriteLine("异步调用");
            SubDelegate sub1 = new SubDelegate(Sub);
            IAsyncResult result1 = sub1.BeginInvoke(2, 3, null, null);
            Console.WriteLine("Go on");
            Console.WriteLine(sub1.EndInvoke(result1));
            Console.ReadKey();

            Console.WriteLine("异步回调");
            SubDelegate sub2 = new SubDelegate(Sub);
            IAsyncResult result2 = sub2.BeginInvoke(5, 6, new AsyncCallback(CallBack), null);
            Console.WriteLine("Go on");
            Console.ReadKey();
        }
        delegate int SubDelegate(int a, int b);
        static int Sub(int a, int b)
        {
            Console.WriteLine("开始计算");
            Thread.Sleep(3000);
            Console.WriteLine("结算完成");
            return a - b;
        }
        static void CallBack(IAsyncResult result)
        {
            SubDelegate sub = (SubDelegate)((AsyncResult)result).AsyncDelegate;
            Console.WriteLine(sub.EndInvoke(result));
        }
        #endregion

 

C#委托调用(同步调用,异步调用,异步回调)

标签:nbsp   back   turn   pre   开始   var   return   call   begin   

原文地址:http://www.cnblogs.com/zxxxx/p/7364889.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!