public void DoClock() { //4. 到达时间时,出现消息 //i. 输入时间 var clockTimeF = Console.ReadLine(); Console.WriteLine(clockTimeF); //ii. 将时间的时分秒存于三个string中 string[] clockTime = new string[3]; for (int i = 0; i < 3; i++) { string temp = ""; int countOfXX = 0; for (int j = 0; j < clockTimeF.Length; j++) { if ((clockTimeF[j] == '/') && countOfXX == i) { clockTime[i] = temp; break; } else if (j == clockTimeF.Length - 1) { temp += clockTimeF[j]; clockTime[i] = temp; } else if (clockTimeF[j] == '/' && countOfXX != i) { temp = ""; countOfXX++; } else { temp += clockTimeF[j]; } } countOfXX = 0; } //iii. 将时间的时分秒存于三个int中 int[] clockTimeToUseClock = new int[3]; for (int i = 0; i < 3; i++) { clockTimeToUseClock[i] = Int32.Parse(clockTime[i]); } //iv. 当时间到达时,发生事件 while (true) { System.Threading.Thread.Sleep(250); if (clockTimeToUseClock[0] == DateTime.Now.Hour && clockTimeToUseClock[1] == DateTime.Now.Minute && clockTimeToUseClock[2] == DateTime.Now.Second) { ClockEventArgs args = new ClockEventArgs(); Clocking(this, args); break; } } }
public void DoClock() { if (alarmClock != null) { ClockEventArgs args = new ClockEventArgs(); alarmClock(this, args); } }
public void SetAlarm(DateTime time) { if (OnTime != null) { ClockEventArgs args = new ClockEventArgs(); args.Time = time; OnTime(this, args); } }
public void SetAlarm(DateTime time) //如果有人注册了这个事件,也就是这个事件不是空 { if (OnTime != null) { ClockEventArgs args = new ClockEventArgs(); args.Time = time; OnTime(this, args); } }
static void ItISOnTime(object sender, ClockEventArgs e) { while (e.Time > DateTime.Now) { System.Threading.Thread.Sleep(1000); } Console.WriteLine("Time out"); while (true) { Console.Beep(); } }
public void DoClock() { string clock = "19:25"; if (ClockAlert != null) { ClockEventArgs args = new ClockEventArgs(); if (DateTime.Now.ToShortTimeString() == clock) { ClockAlert(this, args); } } }
public void StartAlarm() { SetAlarm(); CurrentTime = DateTime.Now; ClockEventArgs args = new ClockEventArgs(); args.span = AlarmTime - CurrentTime; Alarm(this, args); while (CurrentTime < AlarmTime) { System.Threading.Thread.Sleep(500); CurrentTime = DateTime.Now; } }
public void set(DateTime tempTime) { ClockEventArgs args = new ClockEventArgs(); args.dateTime = DateTime.Now; args.clockTime = tempTime; while (args.dateTime < args.clockTime) { System.Threading.Thread.Sleep(1000); if (args.clockTime != args.dateTime) { args.dateTime = DateTime.Now; ClockSet(this, args); } } Console.WriteLine("闹钟时间到!"); }
static void Ring(object sender, ClockEventArgs e) { Console.WriteLine("it's time to get up!!!"); }
static void Alert(Object sender, ClockEventArgs e) { Console.Write("Time's up!"); }
static void ShowTime(object sender, ClockEventArgs e) { Console.WriteLine("闹钟倒计时!"); }
//6. 事件处理方案 static void ShowTimeNow(object sender, ClockEventArgs e) { Console.WriteLine("到时间啦"); }
static void ShowSpan(object sender, ClockEventArgs e) { Console.WriteLine($"还剩{e.span}"); }