第八章(4)-掌握回调的编程技巧-学习笔记

来源:互联网 发布:js语言精粹 编辑:程序博客网 时间:2024/06/05 05:50

通常情况下,我们创建对象之后会马上直接调用它的方法。然而,在有些情况下可能希望在某个场景出现后或某些条件满足后才调用此对象的方法。回调就可以解决这个“延迟调用方法”的问题。这个调用对象的方法称为“回调对象”。

1,基于接口的回调实现

先创建一个回调对象,然后在创建一个控制器对象,将回调对象需要被回调的方法告诉控制器对象。控制器对象负责检查某个场景是否出现或某个条件是否满足,满足则调用回调对象的方法。

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ConsoleApplication3{    public interface ICallback    {        void Run();    }    class CallBackClass:ICallback    {        public void Run()        {            Console.WriteLine(DateTime.Now);        }    }    class Controller    {        private ICallback CallBackObject = null;//回调对象        public Controller(ICallback calbackobject)        {            CallBackObject = calbackobject;        }        public void Begin()        {            Console.WriteLine("敲击任意键显示当前时间,ESC则退出!");            while (Console.ReadKey(true).Key!=ConsoleKey.Escape)            {                CallBackObject.Run();            }        }    }    class Program    {        static void Main(string[] args)        {            ICallback obbj = new CallBackClass();            Controller obj3 = new Controller(new CallBackClass());            Controller obj = new Controller(obbj);            obj.Begin();        }    } }

2,利用委托实现回调

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ConsoleApplication3{    public delegate void ShowTimeD();    class A    {        public void ShowTime()        {            Console.WriteLine(DateTime.Now);        }    }    class B    {        public static void ShowTime()        {            Console.WriteLine(DateTime.Today);        }    }    class Controller    {        private ShowTimeD STD;        public ShowTimeD AddDelegate(ShowTimeD method)        {             return STD += method;        }        public ShowTimeD SubDelegate(ShowTimeD method)        {            return STD -= method;        }        public void CallBack()        {            if (STD!=null)            {                STD.Invoke();            }        }    }    class Program    {        static void Main(string[] args)        {            ShowTimeD sd = null;            Controller controller = new Controller();            A a = new A();            sd=controller.AddDelegate(a.ShowTime);            sd = controller.AddDelegate(B.ShowTime);            while (Console.ReadKey(true).Key != ConsoleKey.Escape)            {                controller.CallBack();            }                    }    } }

3,定时回调

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading;using System.Threading.Tasks;namespace ConsoleApplication3{    //public delegate void TimerCallback(Object state);    class TaskInfo    {        public int count = 0;    }    class Program    {        static void  ShowTime(Object ti)        {            TaskInfo obj = ti as TaskInfo;            obj.count++;            Console.WriteLine("{0}-{1}",obj.count,DateTime.Now);        }        static void Main(string[] args)        {            TaskInfo ti = new TaskInfo();            Timer tm = new Timer(ShowTime,ti,0,1000);            Console.ReadKey();            tm.Dispose();        }    } }

4,多线程回调

在多线程环境中,应用程序主线程通常会启动一些辅助线程在后台王城特定的工作,这就带来了一个问题:“主线程在辅助线程工作结束后如何取回其处理结果”。

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading;using System.Threading.Tasks;namespace ConsoleApplication3{    public class ArrayLst    {        //int[] ary=new int[10];        public int[] MakeArray(int[] ary)        {            Random rnd=new Random();            for (int i = 0; i < ary.Length; i++)            {                ary[i] = rnd.Next(1,100);            }            return ary;        }        public void ShowArray(int[] ary)        {            foreach (int rAry in ary)            {                Console.Write("{0} ",rAry);            }            Console.WriteLine();        }    }    class MyTheard    {        public Func<int[],int[]> createArr;        public Action<int[]> showArr;        public void SortArray(int[] arry)        {            int[] arr = createArr(arry);            Console.WriteLine("原始数据:");            showArr(arr);            int temp = 0;            for (int i = 0; i < arr.Length-1; i++)            {                for (int j = i+1; j < arr.Length; j++)                {                    if (arr[i]>arr[j])                    {                        temp = arr[i];                        arr[i] = arr[j];                        arr[j] = temp;                    }                                  }            }            Console.WriteLine("排序之后:");            showArr(arr);        }    }    class Program    {        static void Main(string[] args)        {            int[] arry = new int[10];            ArrayLst arraylst = new ArrayLst();            MyTheard mine = new MyTheard();            mine.createArr = arraylst.MakeArray;            mine.showArr = arraylst.ShowArray;            mine.SortArray(arry);//这样可以但是新建一个线程的话就不可以了            //这里遇到问题想用辅助线程进行排序            //Thread th = new Thread(mine.SortArray(arry));            //上边的语句不对!!!            //th.Start();            Console.ReadKey();        }    } }


0 0
原创粉丝点击