c# - When I run the program It does not give me an expected output on the console window -
i want create clock class uses delegates notify potential subscribers whenever local time changes value 1 second. when run program. not give me output this. output this, depending on time when run program:
current time: 14:53:56 logging file: 14:53:56 current time: 14:53:57 logging file: 14:53:57 current time: 14:53:58 logging file: 14:53:58
can please me solve problem? here code:
using system; using system.collections.generic; using system.linq; using system.text; using system.threading; using system.threading.tasks; namespace clock_events_delegates { // eventargs base class event data. inherits methods object. // eventargs class empty bucket can use supply information want // event. class hold information event public class timeinfoeventargs : eventargs { public int hour; public int minute; public int second; public timeinfoeventargs(int hour, int minute, int second) { this.hour = hour; this.minute = minute; this.second = second; } } // publisher: class other classes observe. publishes 1 delegate: // secondchanghandler public class clock { private int hour; private int minute; private int second; // delegate subscribers must implement public delegate void secondchangehandler(object clock,timeinfoeventargs timeinformation); // instance of delegate public secondchangehandler secondchanged; // set clock running // raise event each new second public void run() { // infinite loop (; ; ) // sleep second thread.sleep(100); // current time system.datetime dt = system.datetime.now; // if second has changed // notify subscribers if(dt.second != second) { // create timeinfoeventargs object // pass subscriber timeinfoeventargs timeinformation = new timeinfoeventargs(dt.hour, dt.minute, dt.second); // if has subsribed notify them if(secondchanged != null) { secondchanged(this, timeinformation); } } // update state this.second = dt.second; this.minute = dt.minute; this.hour = dt.hour; } } // subscriber: displayclock subscibes clock's events. job of display clock // display currenttime public class displayclock { // given clock, subscribe secondchangedhandler event public void subscribe(clock theclock) { theclock.secondchanged += new clock.secondchangehandler(timehaschanged); } // method implements 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()); } } // second subscriber job write file public class logcurrenttime { public void subscribe(clock theclock) { theclock.secondchanged += new clock.secondchangehandler(writelogentry); } public void writelogentry(object theclock, timeinfoeventargs ti) { console.writeline("logging file: {0}:{1}:{2}", ti.hour.tostring(), ti.minute.tostring(), ti.second.tostring()); } } public class tester { public void run() { // create new clock clock theclock = new clock(); // create display , tell subscribe clock created displayclock dc = new displayclock(); dc.subscribe(theclock); // create log object , tell subscribe clock logcurrenttime lct = new logcurrenttime(); lct.subscribe(theclock); theclock.run(); } } class program { static void main(string[] args) { tester t = new tester(); t.run(); } } }
take on code:
// infinite loop (; ; ) // sleep second thread.sleep(100); // current time system.datetime dt = system.datetime.now;
the following line never execute:
system.datetime dt = system.datetime.now;
because actual loop be:
for (; ; ){ thread.sleep(100); }
so declare loop body using bracets.
Comments
Post a Comment