2010-09-14 37 views

回答

12

你可以使用WaitForExit方法:

if (!p.WaitForExit((int)TimeSpan.FromSeconds(10).TotalMilliseconds)) 
{ 
    // timeout exceeded while waiting for the process to exit 
} 
+0

我喜歡WaitForExit方法!我之前使用過它,但我不知道它有這麼好的超載。我的事情雖然。它需要一個int值作爲參數,所以我們必須將TotalMilliseconds值轉換爲int。 – Alex 2010-09-14 12:13:46

+0

我應該看看過載更多,這是真棒:) – Blam 2010-09-14 12:18:57

+1

@Alex,對Int32參數很好的評論。需要演員陣容。我更新了我的帖子以反映這一點。 – 2010-09-14 12:26:12

1

調用Kill方法的過程變量(在您例如P),這將停止該進程。

注意:你的例子中的循環是非常密集的CPU。您應該使用WaitForExit電話等待10秒(如Darin Dimitrov建議)。

相關問題