私はまだ C# を学んでおり、ここで助けが必要です。datetime が初期化されたかどうかを毎秒チェックするディスパッチタイマーがあります。動作しますが、問題は、datetime が終了するまで同じことをやり続けることです。たとえば、datetime でメモ帳を起動したいとします。問題は、dispatchtimer が 1 秒ごとに datetime を検索することです。これは、メモ帳が 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)
{
}