private void bSave_Click(object sender, EventArgs e) { List <StopDay> stopDays = new List <StopDay>(); try { foreach (DataGridViewRow row in dataGridView1.Rows) { if (row.Cells[0].Value != null && row.Cells[1].Value != null) { StopDay dayelem = new StopDay(); DayOfWeek myDay = (DayOfWeek)Enum.Parse(typeof(DayOfWeek), (string)row.Cells[0].Value.ToString(), true); dayelem.dayofweek = myDay; //dayelem.time = TimeSpan.Parse(row.Cells[1].Value.ToString()); dayelem.time = Convert.ToDateTime(row.Cells[1].Value.ToString()); stopDays.Add(dayelem); } } Properties.Settings.Default["StopSchedule"] = FileHelper.XmlSerializeToString(stopDays); Properties.Settings.Default.Save(); } catch (Exception exp) { MessageBox.Show(exp.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
private void CheckSchedule() { List<Forms.StopDay> stopDays = new List<Forms.StopDay>(); if (Properties.Settings.Default.StopSchedule != "") stopDays = (List<Forms.StopDay>)(FileHelper.XmlDeserializeFromString(Properties.Settings.Default.StopSchedule, stopDays.GetType())); DayOfWeek dayNow = DateTime.Today.DayOfWeek; TimeSpan timeNow = DateTime.Now.TimeOfDay; if (stopDays.Count() != 0) { Forms.StopDay usedStopDay = new Forms.StopDay(); DateTime now = DateTime.Now; TimeSpan shortest = new TimeSpan(7, 0, 0, 0); foreach (Forms.StopDay sd in stopDays) { int daysUntil = ((int)sd.dayofweek - (int)now.DayOfWeek + 7) % 7; //if its today but time has passed its in a week. if (daysUntil == 0 && sd.time.TimeOfDay < now.TimeOfDay) { daysUntil = daysUntil + 7; } TimeSpan timetill = now.AddDays(daysUntil) - now; if (timetill <= shortest) { //if its not today or the time has not passed yet if (timetill.Days != 0 | sd.time.TimeOfDay > now.TimeOfDay) { shortest = (sd.time.TimeOfDay - now.TimeOfDay).Add(timetill); usedStopDay = sd; } } } scheduledTimeLeft = shortest; timerShutDown.Start(); toolStripStatusLabel2.Text = string.Format("Scheduled shutdown: {0}, {1}", usedStopDay.dayofweek, usedStopDay.time.ToString("HH:mm")); } else { toolStripStatusCountdown.Text = "No shutdown scheduled."; toolStripStatusLabel2.Text = ""; } }