void MidnighTimer_Tick(object sender, EventArgs e) { MidnighTimer.Stop(); StartScheduler(); MidnighTimer.Interval = (int)(DateTime.Today.AddDays(1) - DateTime.Now).TotalMilliseconds; MidnighTimer.Start(); }
public void StartScheduler() { RemainingTime = null; CurrentAlarm = null; NextAlarmIfNoAlarmsForToday = ""; NextAlarm = null; RTime = null; var alarm = GetAlarm(GetScheduleFromDate(DateTime.Now)); if (alarm != null) { CurrentAlarm = alarm; TimeSpan alarmTimespan = Convert.ToDateTime(CurrentAlarm.Time).TimeOfDay - DateTime.Now.TimeOfDay; alarmTimer.Interval = Convert.ToInt32(alarmTimespan.TotalMilliseconds); var index = CurrentAlarm.Schedule1.Alarms.OrderBy(c => c.Time) .ToList() .IndexOf(CurrentAlarm); PreviousAlarmTime = DateTime.Now; if (index >= 1) { PreviousAlarmTime = CurrentAlarm.Schedule1.Alarms.OrderBy(z => z.Time).ElementAt(index - 1).Time; } alarmTimer.Start(); EverySecondTimer.Start(); } else { NextAlarm = NextPossibleAlarm(); if (NextAlarm != null && NextAlarm.Any()) { NextAlarmIfNoAlarmsForToday = "Next Alarm At : " + NextPossibleAlarm() .First() .Value .ToString("ddd, MMM d, yyyy") + " " + NextAlarm.First().Key.Time.ToString("h:mm:ss tt"); PreviousAlarmTime = DateTime.Now; EverySecondTimer.Start(); } } MidnighTimer.Interval = (int)(DateTime.Today.AddDays(1) - DateTime.Now).TotalMilliseconds; MidnighTimer.Start(); var schedule = DefaultWeeklySchedule; DefaultWeeklySchedule = null; DefaultWeeklySchedule = schedule; }