C# 异步委托的使用

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;


namespace AsyncDelegateExam
{
    delegate int TakeAWhileDelegate(int data,int ms);


    class Program
    {
        static void Main(string[] args)
        {
            TakeAWhileDelegate del = TakeAWhile;
            IAsyncResult ar = del.BeginInvoke(1, 1000, null, null);
            while (!ar.IsCompleted)
            {
                Console.WriteLine(".");
                Thread.Sleep(50);
            }

            int result = del.EndInvoke(ar);
            Console.WriteLine("result:{0}", result);
            Console.Read();
        }


        static int TakeAWhile(int data,int ms)
        {
            Console.WriteLine("TakeAWhile Started!");
            Thread.Sleep(ms);
            Console.WriteLine("TakeAWhile Completed!");
            return ++data;
        }


        
    }

}


posted @ 2018-04-11 14:11  dxm809  阅读(108)  评论(0编辑  收藏  举报