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


namespace ComprehensiveTest.com
{
    public class AsyCallEx112
    {
        // 定义一个执行加法的委托
        public delegate int sum(int a, int b);
        public class number
        {
            public int m = 4;
            public int numberAdd(int a, int b)
            {
                int c = a + b;
                return c;
            }
            //定义一个与。net framework 定义的asyncCallback委托相对应的回调函数
            public void CallbackMothed2(IAsyncResult ar2)
            {

                //numberadd.BeginInvoke(21, 12, numberBack, numberadd);              //第三个参数为回调  

                sum s = (sum)ar2.AsyncState;//获得BeginInvoke第4个参数
                int number = s.EndInvoke(ar2);//获取运算的结果
                m = number;
                Console.WriteLine("得到的M值:{0}", m);
            }
        }
    }
}


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


namespace ComprehensiveTest.com
{
    public class AsyCallEx113
    {
        //定义一个委托
        public delegate void AsyncEventHanlder();
        public class Class1
        {
            public void Event1()
            {
                Console.WriteLine("Event1 start");
                System.Threading.Thread.Sleep(2000);
                Console.WriteLine("Event1 end");
            }
            public void Event2()
            {
                Console.WriteLine("Event2 start");
                int i = 1;
                while (i < 100)
                {
                    i = i + 1;
                    Console.WriteLine("Event2 " + i.ToString());
                }
                Console.WriteLine("Event2 end");
            }
            public void CallbackMethod( IAsyncResult ar )
            {
                ((AsyncEventHanlder)ar.AsyncState).EndInvoke(ar);
            }
        }
    }
}



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ComprehensiveTest.com;


namespace ComprehensiveTest
{
    class Program
    {
        static void Main(string[] args)
        {
            // 112
            //AsyCallEx112.number num = new AsyCallEx112.number();
            //AsyCallEx112.sum numberadd= new AsyCallEx112.sum(num.numberAdd);
            //AsyncCallback numberBack = new AsyncCallback(num.CallbackMothed2);
            //numberadd.BeginInvoke(21, 12, numberBack, numberadd);
            //Console.WriteLine("得到的结果:");
            //Console.WriteLine(num.m);


            // 113
            long start = 0;
            long end = 0;
            AsyCallEx113.Class1 c = new AsyCallEx113.Class1();
            Console.WriteLine("ready");
            start = DateTime.Now.Ticks;


            AsyCallEx113.AsyncEventHanlder asy = new AsyCallEx113.AsyncEventHanlder(c.Event1);
            //IAsyncResult ia = asy.BeginInvoke(null, null);


            IAsyncResult ia  = asy.BeginInvoke(new AsyncCallback(c.CallbackMethod), asy);
            ia.AsyncWaitHandle.WaitOne();     //对c.Event1产生阻塞,既是等Event1全部执行完毕以后再往后面执行代码(!important)
            c.Event2();
           
            end = DateTime.Now.Ticks;
            Console.WriteLine("时间刻度差= " + Convert.ToString(end - start));
            Console.ReadLine();
        }
    }
}


不知道是什么原因 , 有的时候 , 没法打中文 , 在这个编辑器里面 . 不过现在又好了,,,j_0064.gif

废话少说 , 进入正题了

1 , 这个异步调用( CallBack )  于 传统的callback的区别 , 就是1个在于异步 , 一个在于同步.当然C#异步调用有方法是其变为同步调用 . 如果读者现在不明白何为同步 , 何为异步 , 请找度娘 ......

过程如下 :

        ①:BeginInvoke方法启动异步调用 , 委托实例的方法 , 参数( n个回调参数 , 实例AsyncCallback , 委托实例本身 ) . 其中 : 关于AsyncCallback 的参数是有IAsyncResult的Function  

        ②:EndInvoke检索异步调用的结果 , 就是得到委托运算的结果


使用WaitHandle等待异步调用

 就是产生与传统Callback一样的效果 , 即产生阻塞 , 等待异步调用全部完成 , 进行下面的运行

    由BeginInvoke 产生的IAsyncResult     ( IAsynResult.AsyncWaitHandle.WaitOne() )


请 , 参照上面的代码 , 学习!!!