C# 将控制台输出重定向到winform应用程序中的文本窗口
我从cmd.exe执行了一个批处理文件。我将进程的输出发送到winform上的文本框。文本框似乎仅在进程结束时更新。我希望在流程运行时显示每一行C# 将控制台输出重定向到winform应用程序中的文本窗口,c#,winforms,C#,Winforms,我从cmd.exe执行了一个批处理文件。我将进程的输出发送到winform上的文本框。文本框似乎仅在进程结束时更新。我希望在流程运行时显示每一行 { // prior lines were Process and ProcessInfo setup code startInfo.RedirectStandardOutput = true; startInfo.RedirectStandardError = true; startInfo.CreateNoWind
{
// 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)));
}
`
如何使每一行在创建时都显示出来?谢谢您的帮助。我认为
过程WaitForExit()
是您的问题——它是一个同步调用,会一直阻塞到进程完成。这意味着在进程完成之前,UI线程上不会发生任何事情(包括您BeginInvoked
)。是否调用proc.WaitForExit()代码>从UI线程?