我想启动一个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