2012-09-26 70 views
0

我使用下面的命令來運行批處理文件:設置等待時間進程退出

Process p = new Process(); 
p.StartInfo.UseShellExecute = false; 
p.StartInfo.RedirectStandardOutput = false; 
p.StartInfo.FileName = "d:/my.bat"; 
p.Start(); 
p.WaitForExit(2000000);    
p.Close(); 
p.Dispose(); 

我的問題是,我需要等到上述過程得到完成,並儘快將其關閉,因爲它是可能的。

有什麼建議嗎?

+5

它已經做到這一點。這就是'WaitForExit'所做的。 – Servy

+0

@Servy,但它並沒有幫助我,我需要設置最大值,這是可能的 – GowthamanSS

+2

把'整個位用''block'作爲'Process'實現'IDisposable'。 –

回答

5

您可以使用p.WaitForExit();替換p.WaitForExit(2000000),以管理進程運行時間超過2000000毫秒的情況。

Link

+0

你是對的,但通過我的代碼,在過程中完全徹底移動通過下一組代碼我該如何解決它 – GowthamanSS

2

只需使用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(); 
+0

@ Yannick Blondeau你是我的權利,但通過我的代碼在過程中完全徹底移動通過下一組代碼我如何解決它 - – GowthamanSS

+0

我已經添加了另一種方式來做到這一點。 HTH。 –

+0

它也沒有解決我的問題 – GowthamanSS