2014-01-28 95 views
1

我打電話給Process.Start()運行第三方exe文件。我需要處理這個可執行文件的輸出文件,所以我想調用Process.Start()來阻塞。如何使Process.Start阻止呼叫?

是否可以將此調用更改爲阻塞調用?

Process sampleProcess= new Process(); 
sampleProcess.StartInfo = sampleProcessInfoObject; 
sampleProcess.Start(); 

回答

10

Process.WaitForExit()是你在找什麼。

指示Process組件無限期地等待 相關聯的過程退出。

你會使用這樣的:

Process sampleProcess= new Process(); 
sampleProcess.StartInfo = sampleProcessInfoObject; 
sampleProcess.Start(); 
sampleProcess.WaitForExit(); // Will wait indefinitely until the process exits 
+1

一個簡單的方法:'工藝過程=的Process.Start( 「瀏覽器」); process.WaitForExit();' – Eliko

+0

是的,如果你不需要指定任何東西(把所有內容都默認) - 那麼你不需要一個'ProcessStartInfo'對象。我在這個答案中使用了一個,因爲OP在他們的片段中使用了一個。 –