/// <summary> /// 开启定时关闭线程 /// </summary> private void timingStop() { string dateend = dateover.SelectedDate.ToString(); //结束时间 string dateOverString = this.dateover.SelectedDate.ToString().Substring(0, this.dateover.SelectedDate.ToString().IndexOf(' ')) + " " + timePickerover.TimeSpan; //存在结束时间 if (dateend != null && !dateend.Equals("")) { thrStop = new Thread(() => { long settime = GetTimeString.getSetTime(dateOverString); long nowtime = GetTimeString.getNowTime(); int tim; if ((settime - nowtime).ToString().Length > 9) { tim = 999999999; } else { tim = (int)(settime - nowtime); } Thread.Sleep(tim); clockstop = true; }); thrStop.Start(); } }
/// <summary> /// 定时启动按钮点击事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Button_Click_1(object sender, RoutedEventArgs e) { clockstop = false; pausestop = false; if (examineCan()) { string datestart = this.datestart.SelectedDate.ToString();//开始时间 if (datestart == null || datestart.Equals("")) { MessageBox.Show("请设置日期!!"); return; } else { string dateStartString = this.datestart.SelectedDate.ToString().Substring(0, this.datestart.SelectedDate.ToString().IndexOf(' ')) + " " + timePickerstart.TimeSpan; long settime = GetTimeString.getSetTime(dateStartString); long nowtime = GetTimeString.getNowTime(); if ((settime - nowtime).ToString().Length > 9) { MessageBox.Show("设置时间太长!!"); return; } int tim = (int)(settime - nowtime); if (tim > 0) { reshUi(0); //调整ui //开启倒计时 Thread thr = new Thread(() => { Thread.Sleep(tim); this.Dispatcher.Invoke(new Action(() => { InquireCompany(); timingStop(); })); }); thr.Start(); } else { MessageBox.Show("设置的时间已经过去了!!"); } } } }