我对C#很新,我正在使用Modified WPF应用程序。 我的程序的问题是循环是无限的...... 虽然(现在< end)没有在结束时结束,但它仍然会继续,直到你终止主进程。 该节目正在播放电影和图片,因此每次都不同。这意味着now = now.AddSeconds不起作用。正确?
startupinfo = Listbox
private bool 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)
{
while (now < end)
{
foreach (var selected in startupinfo.SelectedItems)
{
string s = selected.ToString();
if (startupinfoDict.ContainsKey(s))
{
Process process = Process.Start(startupinfoDict[s]);
process.WaitForExit();
while (!process.HasExited)
Thread.Sleep(500);
}
}
foreach (var selected in listBox2.SelectedItems)
{
string s = selected.ToString();
if (listBox2Dict.ContainsKey(s))
{
Process process = Process.Start(listBox2Dict[s]);
process.WaitForExit();
while (!process.HasExited)
Thread.Sleep(500);
}
}
}
return true;
}
return false;
}
答案 0 :(得分:2)
你永远不会在循环中重新评估“现在”。
答案 1 :(得分:1)
您没有在循环中修改now
的值。它始终会将now < end
评估为True。因此无限循环
答案 2 :(得分:1)
now
变量的值永远不会改变。你可以用这种方式循环:
while (DateTime.Now < end.Value)