我还在学习 C#,我需要一些帮助。如果 datetime 已初始化,我有一个 dispatchtimer 每秒检查一次。它可以工作,但问题是它会继续做同样的事情,直到日期时间结束。例如,我希望 datetime 启动记事本。问题是 dispatchtimer 每 1 秒查找一次日期时间,这意味着记事本会第二次启动每个人。当 DateTime 启动它的方法时,如何停止 DispatchTimer?
这是代码:
private void Start_Click(object sender, RoutedEventArgs e) //Button start.
{
test();
}
private void startjob() //DateTime checking for DateValue and start's if correct value.
{
DateTime? start = DateTimePicker1.Value;
DateTime? end = DateTimePicker2.Value;
DateTime now = DateTime.Now;
if (start == null || end == null)
{
Xceed.Wpf.Toolkit.MessageBox.Show("one of the pickers is empty");
}
else if (now >= start.Value && now <= end.Value)
{
Process notePad = new Process();
notePad.StartInfo.FileName = "notepad.exe";
notePad.Start();
}
}
private void test() // DispatchTimer
{
DispatcherTimer dispatcherTimer = new DispatcherTimer();
dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
dispatcherTimer.Interval = new TimeSpan(0, 0, 0, 1);
dispatcherTimer.Start();
}
private void dispatcherTimer_Tick(object sender, EventArgs e)
{
try
{
startjob();
}
catch (Exception)
{
}