public void DoClock() { if (AlarmClock != null) { ClockEventArgs args = new ClockEventArgs(); AlarmClock(this, args); } }
public void TheClock() { while (true) { System.Threading.Thread.Sleep(1000); GetNowTime(); if (IsTime()) { ClockEventArgs args = new ClockEventArgs(); args.alert = true; ClockSound(this, args); } } }
public event ClockEventHandler Warning; //声明事件(实际上Warning成为Clock类的一个属性) public void CheckTime() { ClockEventArgs setClock = new ClockEventArgs(); //初始化参数 setClock.SetTime = Console.ReadLine(); //读取想要设置的闹钟时间 while (true) { string localTime = DateTime.Now.ToShortTimeString().ToString(); if (localTime == setClock.SetTime) //如果对时间进行判断 { Warning(this, setClock); break; } } }
public void StartTime(int cycle) { currentTime = 0; int i = 0; for (; currentTime <= cycle; currentTime++) { ClockEventArgs args = new ClockEventArgs(currentTime); Tick(this, args); if (i < alarmTimes.Length) { if (alarmTimes[i] == currentTime) { Alarm(this, args); } if (alarmTimes[i] < currentTime) { i++; } } Thread.Sleep(1000); } Console.WriteLine("Finished."); }
static void ClockWarning(object sender, ClockEventArgs time) { Console.WriteLine("时间已到!!!"); }
static void ShowAlert(object sender, ClockEventArgs e) { Console.WriteLine("时间到" + e.alert); }
static void Ring(object sender, ClockEventArgs e) { Console.WriteLine("it's time to get up!!!"); }