0

我想从应用程序重定向 c# 中富文本框上的进程输出。但问题是进程的输出在很长一段时间后才显示,直到进程完成其工作。

这是我的代码-

        StringBuilder outputBuilder = new StringBuilder();

        ProcessStartInfo processStartInfo = new ProcessStartInfo();
        processStartInfo.CreateNoWindow = true;
        processStartInfo.RedirectStandardOutput = true;
        processStartInfo.RedirectStandardInput = true;
        processStartInfo.UseShellExecute = false;
        //processStartInfo.WindowStyle = ProcessWindowStyle.Hidden;

        processStartInfo.WorkingDirectory = strHMIModelAppFolder;
        processStartInfo.FileName = ConfigurationHandlerInstance.GetHMIModelApplication();

        Process process = new Process();
        process.StartInfo = processStartInfo;
        process.EnableRaisingEvents = true;

        process.OutputDataReceived += new DataReceivedEventHandler
        (
            delegate(object objSender, DataReceivedEventArgs eventArgs)
            {
                outputBuilder.Append(eventArgs.Data);
                outputBuilder.Append(Environment.NewLine);
                this.SetText(outputBuilder.ToString());
            }
        );

        process.Exited += new EventHandler(ProcExited);
        process.Start();
        process.BeginOutputReadLine();

        //process.WaitForExit();
        //while (!process.HasExited)
        //{
        //    Application.DoEvents();
        //}

        // use the output
        string output = outputBuilder.ToString();
        this.SetText(output);
4

1 回答 1

0

您需要在生成输出时读取它,如下所示:

proc.Start();
while (!proc.StandardOutput.EndOfStream) {
    string line = proc.StandardOutput.ReadLine();
    outputBuilder.Append(line);
    outputBuilder.Append(Environment.NewLine);
    this.SetText(outputBuilder.ToString());
}
于 2013-08-17T07:35:25.393 回答