2013-04-14 18 views
-1

我對C#很陌生,我正在使用修改後的WPF應用程序。 與我的程序的問題是,循環是無限的... 而(現在<結束)不會在結束時結束它仍然會繼續,直到您終止主進程。 該程序顯示電影和圖片,因此每次都有所不同。這意味着現在= now.AddSeconds不起作用。對?while(now <end)在結束時沒有結束,它仍然在無限循環中繼續?

STARTUPINFO =列表框

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

回答

1

您不修改循環中now的值。它將始終將now < end評估爲True。因此無限循環

+0

我應該使用: while(start Sneakybastardd

+0

while(DateTimePicker1.Value <= DateTimePicker2.Value) 也不起作用。 – Sneakybastardd

+0

我會'Datetime.now karthikr

2

你從來沒有刻意去重新評估 「現在」 內循環。

+0

我應該使用: while(start Sneakybastardd

+0

while(DateTimePicker1.Value <= DateTimePicker2.Value) 也不起作用。 – Sneakybastardd

1

您的now變量的值永遠不會改變。你可以用這種方式代替:

while (DateTime.Now < end.Value) 
+0

當我使用那個時,沒有任何反應。我應該使用: while(start Sneakybastardd

+0

while(DateTimePicker1.Value <= DateTimePicker2.Value) 也不起作用。 – Sneakybastardd

+0

@Sneakybastardd沒有人告訴你嘗試任何代碼...你被告知使用'DateTime.Now',這顯然返回NOW的值。如果「當我使用那個時沒有任何反應」,那麼你應該明白爲什麼,也許檢查'DateTimePicker'中的值。 – Andrei