// Set the clock running, it will raise an // event for each new second public void Run(int seconds) { for (int i=0; i<= seconds; i++) { // Sleep 1 Second Thread.Sleep(1000); // Get the current time System.DateTime dt = System.DateTime.Now; // If the second has changed // notify the subscribers if (dt.Second != _second) { // Create the TimeInfoEventArgs object // to pass to the subscribers TimeInfoEventArgs timeInformation = new TimeInfoEventArgs( dt.Hour, dt.Minute, dt.Second); // If anyone has subscribed, notify them OnSecondChange(this, timeInformation); } // update the state _second = dt.Second; _minute = dt.Minute; _hour = dt.Hour; } }
// This method should write to a file // we write to the console to see the effect // this object keeps no state public void WriteLogEntry(object theClock, TimeInfoEventArgs ti) { Console.WriteLine("Subscriber DisplayTimeOfClock, received notify from Clock that second changed, log to console: {0}:{1}:{2}", ti.hour.ToString(), ti.minute.ToString(), ti.second.ToString()); }
// The method which fires the Event protected void OnSecondChange(object clock, TimeInfoEventArgs timeInformation) { //Do something first Console.WriteLine("Time from clock: {0}:{1}:{2}", _hour.ToString(), _minute.ToString(), _second.ToString()); Console.WriteLine("Time changed, subscribers will be notified."); // Check if there are any Subscribers, if there are, notify them through SecondChangeEvent if (SecondChangeEvent != null) { // Call the Event SecondChangeEvent(clock, timeInformation); } }
async void TimeChanged(object sender, TimeInfoEventArgs e) { }
// This method should write to a file // we write to the console to see the effect // this object keeps no state public void WriteLogEntry(object theClock, TimeInfoEventArgs ti) { Console.WriteLine("Logging to file: {0}:{1}:{2}", ti.hour.ToString(), ti.minute.ToString(), ti.second.ToString()); }
// The method that implements the // delegated functionality public void TimeHasChanged(object theClock, TimeInfoEventArgs ti) { Console.WriteLine("Current Time: {0}:{1}:{2}", ti.hour.ToString(), ti.minute.ToString(), ti.second.ToString()); }
// The method which fires the Event protected void OnSecondChange(object clock, TimeInfoEventArgs timeInformation) { // Check if there are any Subscribers // Call the Event SecondChange?.Invoke(clock, timeInformation); }
public void OnFifteenChange(Object o, TimeInfoEventArgs e) { Console.WriteLine("15 Segundos"); }
public void TimeHasChanged(object theClock, TimeInfoEventArgs ti) { Console.WriteLine("Current Time:"); }