public void Run() { for (;;) { Thread.Sleep(100); DateTime dt = DateTime.Now; //notify to the subscriber if (dt.Second != second) { TimeEventArgs args = new TimeEventArgs(dt.Hour, dt.Second, dt.Minute); if (listOfHandlers != null)//if anyone has subscribed then notify them; { listOfHandlers(this, args); } } this.hour = dt.Hour; this.minute = dt.Minute; this.second = dt.Second; } }
//event handler that matches signature with publisher's deleagte and implements publisher's delegate public void LoggingTime(object clock, TimeEventArgs args) { Console.WriteLine("Logging in the logger, logging time => {0}::{1}::{2}", args.hour, args.minute, args.second); }
//event handler, whose signature matches with the delegate, and implements the publisher's deleagted functionality public void TimeHasChanged(object clock, TimeEventArgs args) { Console.WriteLine("The Time is {0}::{1}::{2}", args.hour, args.minute, args.second); }