C#_delegate EndInvoke

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading;



//异步调用

//returntype EndInvoke(ref_out_argument,AsyncWaitHandle ar)



namespace Starter

{

    public delegate int DeleageteClass(out DateTime start, out DateTime stop);



    class Program

    {

        static void Main(string[] args)

        {

            DeleageteClass del = MethodA;

            DateTime start;

            DateTime stop;



            IAsyncResult ar = del.BeginInvoke(out start, out stop, null, null);



            ar.AsyncWaitHandle.WaitOne();

            

            //do something else

           

           

            //EndInvoke返回一个一步结果

            int elapse = del.EndInvoke(out start, out stop, ar);



            Console.WriteLine("start: "+ start.ToLongTimeString());

            Console.WriteLine("stop: " + stop.ToLongTimeString());

            Console.WriteLine("elapse: " + elapse);



            Console.ReadLine();

        }







        public static int MethodA(out DateTime start,out DateTime stop)

        {

            start = DateTime.Now;

            Thread.Sleep(5000);

            stop = DateTime.Now;

            return (stop-start).Seconds;

        }

    }



}


你可能感兴趣的:(delegate)