private void TimespanUpdateTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { Dispatcher.BeginInvoke(new Action(() => { try { // Load Current Day Start and Day End var d = DateTime.Now; var dayStart = new DateTime(d.Year, d.Month, d.Day, 0, 0, 0, DateTimeKind.Local); var dayEnd = dayStart.AddDays(1); DateTime from = DateTime.MinValue; DateTime to = DateTime.MinValue; // Set From if (FromTime > dayStart) { from = FromTime; } else if (FromTime > DateTime.MinValue) { from = new DateTime(dayStart.Year, dayStart.Month, dayStart.Day, FromTime.Hour, 0, 0); } else { from = dayStart; } // Set To if (ToTime > dayStart && ToTime <= dayEnd) { to = ToTime; } else if (ToTime > DateTime.MinValue && ToTime.Hour > 0) { to = new DateTime(dayStart.Year, dayStart.Month, dayStart.Day, ToTime.Hour, 0, 0); } else { to = dayEnd; } // Update Timespan if (FromTime != from || ToTime != to) { FromDateTimes.Clear(); for (var x = 0; x <= 24; x++) { FromDateTimes.Add(dayStart.AddHours(x)); } FromTime = from; // Load ToDateTimes UpdateToTimes(); ToTime = to; } } catch (Exception ex) { log.Error(ex); } }), System.Windows.Threading.DispatcherPriority.Background, new object[] { }); }
private void LoadTimespan() { // Load Current Day Start and Day End var d = DateTime.Now; var dayStart = new DateTime(d.Year, d.Month, d.Day, 0, 0, 0, DateTimeKind.Local); var dayEnd = dayStart.AddDays(1); FromDateTimes.Clear(); for (var x = 0; x <= 24; x++) { FromDateTimes.Add(dayStart.AddHours(x)); } // Load From Setting string fromSetting = Properties.Settings.Default.FromTime; DateTime savedFrom = DateTime.MinValue; DateTime.TryParse(fromSetting, out savedFrom); // Load To Setting string toSetting = Properties.Settings.Default.ToTime; DateTime savedTo = DateTime.MinValue; DateTime.TryParse(toSetting, out savedTo); // Set From if (savedFrom > dayStart) { FromTime = savedFrom; } else if (savedFrom > DateTime.MinValue) { FromTime = new DateTime(dayStart.Year, dayStart.Month, dayStart.Day, savedFrom.Hour, 0, 0, DateTimeKind.Local); } else { FromTime = dayStart; } // Load ToDateTimes UpdateToTimes(); // Set To if (savedTo > dayStart && savedTo < dayEnd && savedTo > FromTime) { ToTime = savedTo; } else if (savedTo > DateTime.MinValue && savedTo.Hour > 0) { ToTime = new DateTime(dayStart.Year, dayStart.Month, dayStart.Day, savedTo.Hour, 0, 0, DateTimeKind.Local); } else { ToTime = dayEnd; } // Start/Restart TimespanUpdateTimer if (timespanUpdateTimer != null) { timespanUpdateTimer.Stop(); } else { timespanUpdateTimer = new System.Timers.Timer(); timespanUpdateTimer.Interval = 5000; timespanUpdateTimer.Elapsed += TimespanUpdateTimer_Elapsed; } timespanUpdateTimer.Start(); }