我有以下命令从cmd.exe执行批处理文件。我将进程的输出转到winform上的文本框中。文本框似乎仅在进程结束时更新。我想在进程运行时显示每一行。
{
// prior lines were Process and ProcessInfo setup code
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.CreateNoWindow = true;
startInfo.UseShellExecute = false;
// see below for output handler
Process proc = Process.Start(startInfo);
proc.ErrorDataReceived += proc_DataReceived;
proc.OutputDataReceived += proc_DataReceived;
proc.BeginErrorReadLine();
proc.BeginOutputReadLine();
proc.WaitForExit();
}
void proc_DataReceived(object sender, DataReceivedEventArgs e)
{
// output will be in string e.Data
if (e.Data != null)
BeginInvoke(new Action(() => textBox1.Text += (Environment.NewLine + e.Data)));
}
` 如何在创建每条线时显示它?谢谢你的帮助。
答案 0 :(得分:2)
我认为proc.WaitForExit();
是你的问题 - 它是一个同步调用,直到进程完成才会阻塞。这意味着在完成该过程之前,UI线程(包括您BeginInvoked
的所有内容)都不会发生任何事情。