Clock based on event delegation multithreading

announcer:

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

namespace clock
{
    class Clock // public makes it inheritable, publisher class
    {
        private int _hour;
        private int _minute;
        private int _second;
        public delegate void SecondChangeHandler (object clock, TimeInfoEventArgs timeInformation, int outdata); // Can be customized Principal agent

        public event SecondChangeHandler SecondChange; // Define the event, the event encapsulates the delegate, and becomes the delegate of the event type. Most existing definitions

        protected void OnSecondChange (object clock, TimeInfoEventArgs timeInformation) {// Define the trigger event
            if (SecondChange! = null) {// Event has been subscribedSecondChange
                (clock, timeInformation, 1); // Execution delegate method via event
            }
        }

        public void Run () {// Trigger event under certain conditions
            for (;;) {

                Thread.Sleep(1000);
                System.DateTime dt = System.DateTime.Now;
                if (dt.Second != _second) {
                    TimeInfoEventArgs timeInformation = new TimeInfoEventArgs(dt.Hour,dt.Minute,dt.Second);
                    OnSecondChange(this,timeInformation);
                }
                _second = dt.Second;
                _minute = dt.Minute;
                _hour = dt.Hour;
            }
        }
    }

    public class TimeInfoEventArgs
    {
        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;
        }
    }
}

Subscriber (Observer Mode):

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

namespace clock
{
    class DisplayClock // Observer class (subscriber)
    {
        public void Subscribe (Clock theClock) {
            theClock.SecondChange + = new Clock.SecondChangeHandler (TimeHasChange); // Register the custom method to the event of the class through the proxy agent , Subscribe to the event, that is, first associate the method to the delegate, and then register the delegate to the event
        }

        private void TimeHasChange(object clock, TimeInfoEventArgs timeInformation,int outdata)//订阅事件后自生成事件处理函数来响应事件
        {
            //throw new NotImplementedException();
            Console.WriteLine("current time:{0}:{1}:{2}", timeInformation.hour.ToString(),
                timeInformation.minute.ToString(),
                timeInformation.second.ToString());
        }
    }
    class LogClock {
        public void Subscribe(Clock theClock)
        {
            theClock.SecondChange += new Clock.SecondChangeHandler(WriteLogentry);
            theClock.SecondChange += new Clock.SecondChangeHandler(showclocl);
        }

        private void showclocl(object clock, TimeInfoEventArgs timeInformation, int outdata)
        {
            Console.WriteLine("show time:{0}:{1}:{2}", timeInformation.hour.ToString(),
                timeInformation.minute.ToString(),
                timeInformation.second.ToString());
            //throw new NotImplementedException();
        }

        private void WriteLogentry(object clock, TimeInfoEventArgs timeInformation,int outdata)
        {
            //throw new NotImplementedException();
            Console.WriteLine("Logging to file:{0}:{1}:{2}",timeInformation.hour.ToString(),
                timeInformation.minute.ToString(),
                timeInformation.second.ToString());
        }
    }
}

 

Guess you like

Origin www.cnblogs.com/80028366local/p/12760732.html