我想创建一个时钟类,它使用委托在本地时间将其值更改一秒时通知潜在订阅者。当我运行程序时。它没有给我这样的输出。输出看起来像这样,取决于运行程序的时间:
Current Time: 14:53:56
Logging to file: 14:53:56
Current Time: 14:53:57
Logging to file: 14:53:57
Current Time: 14:53:58
Logging to file: 14:53:58
任何人都可以帮我解决这个问题吗?这是代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Clock_Events_Delegates
{
// EventArgs is the base class for all event data. It inherits all its methods from Object.
// EventArgs class is an empty bucket that you can use to supply any information you want
// about the event. a class to hold the information about the 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;
}
}
// The publisher: the class that other classes will observe. This publishes one delegate:
// SecondChangHandler
public class Clock
{
private int hour;
private int minute;
private int second;
// the delegate the subscribers must implement
public delegate void SecondChangeHandler(object clock,TimeInfoEventArgs timeInformation);
// an instance of the delegate
public SecondChangeHandler SecondChanged;
// Set the clock running
// It will raise an event for each new second
public void Run()
{
// an infinite loop
for (; ; )
// Sleep for a second
Thread.Sleep(100);
// 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 subscriber
TimeInfoEventArgs timeInformation = new TimeInfoEventArgs(dt.Hour, dt.Minute, dt.Second);
// if anyone has subsribed notify them
if(SecondChanged != null)
{
SecondChanged(this, timeInformation);
}
}
// update the state
this.second = dt.Second;
this.minute = dt.Minute;
this.hour = dt.Hour;
}
}
// A subscriber: DisplayClock subscibes to the clock's events. The job of the Display Clock is
// to display the currentTime
public class DisplayClock
{
// given a clock, subscribe to its SecondChangedHandler event
public void Subscribe(Clock theClock)
{
theClock.SecondChanged += new Clock.SecondChangeHandler(TimeHasChanged);
}
// method that 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());
}
}
// a second subscriber whose job is to write to a 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 to file: {0}:{1}:{2}",
ti.hour.ToString(), ti.minute.ToString(), ti.second.ToString());
}
}
public class Tester
{
public void Run()
{
// Create a new clock
Clock theClock = new Clock();
// create the display and tell it subscribe to the clock just created
DisplayClock dc = new DisplayClock();
dc.Subscribe(theClock);
// create the log object and tell it to subscribe to the clock
LogCurrentTime lct = new LogCurrentTime();
lct.Subscribe(theClock);
theClock.Run();
}
}
class Program
{
static void Main(string[] args)
{
Tester t = new Tester();
t.Run();
}
}
}
答案 0 :(得分:2)
看看这段代码:
// an infinite loop
for (; ; )
// Sleep for a second
Thread.Sleep(100);
// get the current time
System.DateTime dt = System.DateTime.Now;
以下行永远不会执行:
System.DateTime dt = System.DateTime.Now;
因为实际的循环是:
for (; ; ){
Thread.Sleep(100);
}
所以只需使用支撑声明正确的循环体。