我想啓動一個cmd文件並立即得到輸出。重定向輸出的進程
看看我的代碼。導入process.WaitForExit()
不等;爲什麼不? copyf.cmd
運行良好,如果我不以隱藏模式啓動它,因爲顯示的dosbox運行到cmd的末尾。 在隱藏模式下,cmd被關閉,因爲process.WaitForExit()
不會完成,直到cmd完成。
public void doSomeThing( Queue<string> output, // queue for output log
Queue<string> error // queue for error log
)
{
String com = "some params";
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.FileName = Properties.Settings.Default.pathTo + @"\Make\copyf.cmd";
startInfo.Arguments = com;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = true;
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
process.StartInfo = startInfo;
Thread thread = new Thread(new ThreadStart(() =>
{
String er;
String outp;
while (true)
{
outp = process.StandardOutput.ReadLine();
if(outp != null)
output.Enqueue("Output :" + outp + "\n");
er = process.StandardError.ReadLine();
if (er != null)
error.Enqueue("Error :" + er + "\n");
}
}));
process.Start();
thread.Start();
process.WaitForExit();
}
爲什麼你認爲'WaitForExit'不等待進程退出? – Ryan 2013-04-21 21:35:51
我發現'.WaitForExit()'不能工作交叉線程,相反我相信我用'while(!proc.HasExited)'(想法比評論更有意思) – Sayse 2013-04-21 21:37:08
好吧,它不是交叉線程。該線程只讀取輸出/錯誤。 「進程」runnig在process.WaitForExit(9被調用。 – tux007 2013-04-21 21:39:00