2014-03-06 24 views
-1

我正在使用Visual C#2010(.NET Framework 3.5)創建一個GUI應用程序。如何將此代碼更改爲無停止響應?在for循環中處理WaitForExit時掛起

for (int i = listBox1.Items.Count - 1; i >= 0; i--) 
{ 
    listBox1.SelectedIndex = i; 
    Process myProcess = new Process(); 
    myProcess.StartInfo.FileName = "ffmpeg.exe"; 
    myProcess.StartInfo.WorkingDirectory = ""; 
    myProcess.StartInfo.Arguments = " -i \"" + listBox1.SelectedItem + "\" " + "-y "; 
    myProcess.StartInfo.CreateNoWindow = true; 
    myProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; 
    myProcess.Start(); 
    myProcess.WaitForExit(); 
    listBox1.Items.RemoveAt(i); 
} 

轉換程序一段時間使用轉換。如何顯示正在運行的應用程序的新窗體或messengebox?我想讓它響應窗口。 歷史的循環: How do I do I loop through items in a list box and then remove that item?

+2

的可能重複(http://stackoverflow.com/questions/22151235/in-c -sharp-how-do-you-read-from-dos-program-you-executed-if-the-program-is-const) – I4V

回答

7

WaitForExit,勿庸置疑,會阻塞線程,直到進程已退出。
或者,您可以使用Exited事件在進程退出時收到通知。

您還需要將EnableRaisingEvents屬性設置爲true以便激發Exited事件。

myProcess.EnabledRaisingEvents = true; 
myProcess.Exited += new EventHandler(myProcess_Exited); 
myProcess.Start(); 

void myProcess_Exited(object sender, EventArgs e) 
{ 
    //process has exited, implement logic here 
    listBox1.Items.Remove... 
} 
+2

* WaitForExit,不出所料,會阻塞線程直到進程退出* Mind = Blown –

+0

+1誰知道...誰知道... –

+0

開始我的過程使用選定的列表框項目,如果退出過程開始新的過程,並使用列表框中的下一個選擇。同時結束列表框項目。 – Gabee8

0

謝謝答覆! 完成此代碼:?在C#中如何從程序是否持續輸出你執行DOS程序的讀取]

 void myProcess_Exited(object sender, EventArgs e) 
    { 
     this.Invoke((MethodInvoker)delegate 
     { 

      if (listBox1.SelectedItem != null) 
      { 
       listBox1.Items.RemoveAt(0); 
      } 
      if (listBox1.Items.Count > 0) 
      { 
       button1.PerformClick(); 
      } 
      else 
      { 
       MessageBox.Show("All Done!!"); 
      } 
     }); 
    }