日期:2014-05-19  浏览次数:20685 次

关于回调函数 不懂!!!!!!!!!!!!!!!!!!!
Fun   是我的工作函数   我用委托来异步调用,这个回调函数会在Fun结束后调用   而且是在调用Fun的这个线程中   我就不懂了   为什么不直接把回调函数的内容加到Fun里,而非要用这个CallBack函数呢?CallBack不是紧跟在Fun结束就调用吗?
我在这里完全看不出回调函数有什么作用     请哪位给我讲解一下!
谢谢了!!!

class   Program
        {
                private   delegate   void   IFun();

                public   static   void   Fun()
                {
                        Console.WriteLine( "Fun   Start,the   Fun 's   Thread   is   {0} ",Thread.CurrentThread.ManagedThreadId);
                        Thread.Sleep(1000);
                        Console.WriteLine( "Fun   End ");
                       
                }
                public   static   void   CallBack(IAsyncResult   iar)
                {
                        Console.WriteLine( "CallBack   Start,the   Thread   is   {0} ",Thread.CurrentThread.ManagedThreadId);
                        IFun   iFun   =   (IFun)(iar.AsyncState);
     
                        Console.WriteLine( "CallBack   End ");
                }

             

                static   void   Main(string[]   args)
                {
                        Console.WriteLine( "Main   Thread   is   {0} ",   Thread.CurrentThread.ManagedThreadId);
                        IFun   iFun   =   new   IFun(Fun);
                        IAsyncResult   iar   =   iFun.BeginInvoke(CallBack,iFun);

                       
                        Console.WriteLine( "Main   is   Over ");
                        Console.Read();
                }
        }

------解决方案--------------------
一个比较简单直接的例子,你看AcdSee里看第一张图片的时候,第二张是后台自动加载的(是不是开一个线程不知道),也就是说你看第一张,跟他自动加载第二张是异步执行的(通俗点就是互不干扰)