Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This is working for me:</p> <pre><code>private void button1_Click(object sender, EventArgs e) { using (Process sortProcess = new Process()) { sortProcess.StartInfo.FileName = @"F:\echo_hello.bat"; sortProcess.StartInfo.CreateNoWindow = true; sortProcess.StartInfo.UseShellExecute = false; sortProcess.StartInfo.RedirectStandardOutput = true; // Set event handler sortProcess.OutputDataReceived += new DataReceivedEventHandler(SortOutputHandler); // Start the process. sortProcess.Start(); // Start the asynchronous read sortProcess.BeginOutputReadLine(); sortProcess.WaitForExit(); } } void SortOutputHandler(object sender, DataReceivedEventArgs e) { Trace.WriteLine(e.Data); this.BeginInvoke(new MethodInvoker(() =&gt; { richTextBox1.AppendText(e.Data ?? string.Empty); })); } </code></pre> <p><a href="http://msdn.microsoft.com/query/dev10.query?appId=Dev10IDEF1&amp;l=EN-US&amp;k=k%28SYSTEM.DIAGNOSTICS.PROCESS.BEGINOUTPUTREADLINE%29;k%28TargetFrameworkMoniker-%22.NETFRAMEWORK,VERSION=V4.0%22%29;k%28DevLang-CSHARP%29&amp;rd=true" rel="nofollow">The example you started with</a> was a console application, which doesn't care much about multithreaded access. For Windows Forms when you update a control this has to be done from the main UI thread, which is why <code>BeginInvoke</code> is needed. If you want to check rapidly if a handler like <code>SortOutputHandler</code> is working properly you can use <code>System.Diagnostics.Trace.Write*</code>, which doesn't need <code>BeginInvoke</code>.</p> <p><strong>EDIT:</strong> <code>echo_hello.bat</code> simply echoes the "hello" string:</p> <pre><code>@echo off echo hello </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload