第508篇-Delegate和Event异同--(内容篇4:共6篇)

本文用一个动态变化的clock,模拟时钟的数据变化,即每一次事件触发,都会发送给subscriber不同的值。

namespace EventKeyWord
{
    /// <summary>
    /// Summary description for Class1.
    /// </summary>
    class Test
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            //
            // TODO: Add code to start application here
            //
            Clock clk = new Clock();
            DisplayClock dc = new DisplayClock();
            dc.Subscribe(clk);

            LogClock mlogClock = new LogClock();
            mlogClock.Subscribe(clk);

            clk.run();

            Console.ReadKey();

            //if no event in line 45,the OnSecondChange could be invoked directly here
        }
    }
    public class TimeInfoEventArgs : EventArgs
    {
        public readonly int hour;
        public readonly int minute;
        public readonly int second;
        public TimeInfoEventArgs(int hour, int minute, int second)
        {
            this.hour = hour;
            this.minute = minute;
            this.second = second;
        }
    }

    public class Clock
    {
        public delegate void SecondChange(object Clock, TimeInfoEventArgs args);

        private int second;
        public event SecondChange OnSecondChange;//the keyword event is optional

        public Clock()
        {
            second = System.DateTime.Now.Second;
        }
        public void run()
        {
            while (true)
            {
                // Delay一下,造成前后datetime值不同.
                System.Threading.Thread.Sleep(500);
                DateTime dt = System.DateTime.Now;
                if (dt.Second != second)
                    if (OnSecondChange != null)
                    {
                        OnSecondChange(this, new TimeInfoEventArgs(dt.Hour, dt.Minute, dt.Second));
                    }
                second = System.DateTime.Now.Second;
            }
        }
    }


    /// <summary>
    /// a class to display the current clock
    /// </summary>
    public class DisplayClock
    {
        public void Subscribe(Clock clk)
        {
            clk.OnSecondChange += new Clock.SecondChange(this.displayClock);
        }
        public void displayClock(object Clock, TimeInfoEventArgs args)
        {
            Console.WriteLine("displayClock now:");
            Console.WriteLine(args.hour + ":" + args.minute + ":" + args.second);
        }
    }

    /// <summary>
    /// a second class to log the current clock
    /// </summary>
    public class LogClock
    {
        public void Subscribe(Clock clk)
        {
            clk.OnSecondChange += new Clock.SecondChange(this.logsClock);
            //if no event in line 45,this could be clk.OnSecondChange+=new Clock.SecondChange(this.logsClock); and the displayClock won't work
        }

        public void logsClock(object Clock, TimeInfoEventArgs args)
        {
            Console.WriteLine("logClock now:");
            Console.WriteLine(args.hour + ":" + args.minute + ":" + args.second);
        }
    }
}

运行效果:

posted @ 2013-03-04 18:02  Shanghai Jim Zhou  阅读(218)  评论(0编辑  收藏  举报