I'm trying to do console output in RichTextBox . Here is my code:

 public void Run() { Process myProcess = new Process(); myProcess.StartInfo.FileName = @"start.bat"; myProcess.StartInfo.CreateNoWindow = true; myProcess.StartInfo.UseShellExecute = false; myProcess.StartInfo.RedirectStandardOutput = true; myProcess.OutputDataReceived += proc_OutputDataReceived; myProcess.Start(); myProcess.BeginOutputReadLine(); } public void proc_OutputDataReceived(object sender, DataReceivedEventArgs e) { this.Invoke(new Action(() => richTextBoxConsole.Text += (e.Data + Environment.NewLine))); } 

But in RichTextBox , only the first line from the console is displayed. And It is necessary to display all the lines in real time. Where is the mistake?

  • Do you have all console messages go through "myProcess.OutputDataReceived"? - Luchunpen
  • yes, all console messages go - asdk
  • Logically, everything is correct. Try to make a static console on richTextBox, just try additionally save console messages to a string and call the text console directly. - Luchunpen
  • 3
    The above code is working, everything is fine displayed. The problem could not be reproduced. Tested in WinForms controls. Most likely you need to check the batch file for its output when running in a standard console without redirect. Well, the encoding is not taken into account here, so there will be cracks in the output. - rdorn
  • one
    better not use simple concatenation richTextBoxConsole.Text += Use richTextBoxConsole.AppendText() - Sublihim

1 answer 1

there is almost a ready-made solution in documentation

https://msdn.microsoft.com/en-us/library/system.diagnostics.process.outputdatareceived.aspx

subscribe to the appropriate event and simply duplicate the text on this event in the desired field