只需使用WaitForExit
不喜歡任何參數:
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = false;
p.StartInfo.FileName = "d:/my.bat";
p.Start();
p.WaitForExit();
p.Close();
p.Dispose();
它會等到你的過程就完成了。有關更多信息,請參見the documentation on MSDN。
另外,特別是如果你想給反饋給用戶,你可以做這樣的事情:
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = false;
p.StartInfo.FileName = "d:/my.bat";
Console.Write("Running {0} ", p.StartInfo.FileName)
p.Start();
while (!p.HasExited)
{
Console.Write(".");
// wait one second
Thread.Sleep(1000);
}
Console.WriteLine(" done.");
p.Close();
p.Dispose();
它已經做到這一點。這就是'WaitForExit'所做的。 – Servy
@Servy,但它並沒有幫助我,我需要設置最大值,這是可能的 – GowthamanSS
把'整個位用''block'作爲'Process'實現'IDisposable'。 –