void MyAlarm(object sender, TimeArgs args) { for (int i = 0; i < 10; i++) { Console.WriteLine("滴!滴!滴!滴!滴!滴!滴!滴!滴!"); } }
static void Main(string[] args) { MyAlarmClock CLK = new MyAlarmClock(); TimeArgs AlarmTime = new TimeArgs() { hour = 0, minute = 1, second = 0 }; CLK.Clock1.Run(AlarmTime); }
public event ClockAlarm Alarm; //定义事件 public void Run(TimeArgs AlarmTime) //闹钟启动 { TimeArgs CurrentTime = new TimeArgs() { hour = 0, minute = 0, second = 0 }; //当前时间 while (true) { if (IsTime(AlarmTime, CurrentTime)) { Alarm(this, AlarmTime); } Tick(this, CurrentTime); System.Threading.Thread.Sleep(1000); } }
void MyTick(object sender, TimeArgs args) { Console.WriteLine("当前时间:[" + args.hour + ":" + args.minute + ":" + args.second + "]"); if (args.second < 59) { args.second++; } else { args.second = 0; args.minute++; if (args.minute == 60) { args.minute = 0; args.hour = (args.hour + 1) % 24; } } }
public bool IsTime(TimeArgs a, TimeArgs b) { return(a.hour == b.hour && a.minute == b.minute && a.second == b.second); }