public void SetATime() { Console.WriteLine("请输入响铃时间,如:2019-03-20 11:00:00"); try { string setTime = Console.ReadLine(); ringTime = Convert.ToDateTime(setTime); Console.WriteLine("闹钟已设置为: " + setTime); while (true) { nowTime = new System.DateTime(); nowTime = System.DateTime.Now; if (DateTime.Compare(nowTime, ringTime) >= 0) { ALARMEventArgs args = new ALARMEventArgs() { hour = nowTime.Hour, minute = nowTime.Minute, second = nowTime.Second }; OnClock(this, args); break; } } } catch (Exception e) { Console.WriteLine(e.Message); } }
static void Ring(object sender, ALARMEventArgs args) { Console.WriteLine($"时间到了!"); }