-1

我对 C# 很陌生,我正在使用修改后的 WPF 应用程序。我的程序的问题是循环是无限的......而(现在<结束)并没有在结束时间结束它仍然继续直到你终止主进程。该程序正在显示电影和图片,因此每次都不同。这意味着 now = now.AddSeconds 不起作用。对?

启动信息 = 列表框

    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;
    }
4

3 回答 3

2

您永远不会费心在循环中重新评估“现在”。

于 2013-04-14T18:05:47.110 回答
1

您没有修改now循环中的值。它将始终评估now < end为 True。因此无限循环

于 2013-04-14T18:06:17.317 回答
1

你的now变量的值永远不会改变。你可以这样循环:

while (DateTime.Now < end.Value)
于 2013-04-14T18:06:28.493 回答