public void ClockOn(ClockEventArgs args) { DateTime settime = new DateTime(args.year, args.month, args.day, args.hour, args.minute, args.second); if (ClockRemind != null) { int n = 0; while (true) { DateTime today = DateTime.Now; while (today > settime) { n++; if (n == 1) { break; } } if (n == 1) { break; } } ClockRemind(this, args); } }
static void Main(string[] args) { Clock clock = new Clock(); clock.ClockRemind += ClockRemindFun; ClockEventArgs obj = new ClockEventArgs(2018, 10, 7, 10, 10, 10);//设置闹钟时间 clock.ClockOn(obj); }
public event ClockEventHandler Warning; //声明事件 public void Time() { ClockEventArgs setClocktime = new ClockEventArgs(); setClocktime.SetTime = Console.ReadLine(); while (true) { string systemTime = DateTime.Now.ToShortTimeString().ToString(); //获取系统时间 if (systemTime == setClocktime.SetTime) { Warning(this, setClocktime); break; } } }
public void DoSetClock(ref DateTime tempTime) { ClockEventArgs args = new ClockEventArgs(); args.clockTime = tempTime; args.realTime = DateTime.Now; if (args.clockTime < args.realTime) { throw new ArgumentOutOfRangeException("error setTime!"); } while (args.clockTime > args.realTime) { //时间逐渐接近,用延迟一会来代替 System.Threading.Thread.Sleep(500); if (args.clockTime != args.realTime) { args.realTime = DateTime.Now; SetClock(this, args); } } Console.WriteLine("闹钟时间到!"); }
static void ClockRemindFun(object sender, ClockEventArgs e) { Console.WriteLine("闹钟时间到!"); }
//事件处理方法 static void ClockWarning(object sender, ClockEventArgs time) { Console.WriteLine("时间到"); }
static void Clock_SetClock(object sender, ClockEventArgs e) { Console.WriteLine("闹钟时间未到......"); }