我想启动一个 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();
}