Note that there are some explanatory texts on larger screens.

plurals
  1. POc# run CL exe or batch cpture output LIVE to textbox?
    primarykey
    data
    text
    <p>Is it possible to run any Command Line based program or batch file and capturer (re-direct) the out put to a text box LIVE</p> <p>the CL takes time and it produce text!</p> <p>something like tracert.exe (it takes time and produce good amount of text).</p> <p>actually I will work with tracert.exe and I like to capture the output live and show it in a text-box while it is running</p> <p>EDIT: My problem is to have it LIVE I mean any new line or char the console produce will be sent/pulled to/by the textBox NOT until the program is done!</p> <p>Simply I want to build is exactly like this <a href="http://www.codeproject.com/KB/threads/redir.aspx" rel="nofollow noreferrer">http://www.codeproject.com/KB/threads/redir.aspx</a> (check the demo) but in C# </p> <p>here is my code:</p> <pre><code>private void button1_Click(object sender, EventArgs e) { Process pc = new Process(); pc.StartInfo.FileName = "tracert.exe"; pc.StartInfo.Arguments = "google.com"; pc.StartInfo.UseShellExecute = false; pc.StartInfo.RedirectStandardOutput = true; pc.StartInfo.CreateNoWindow = true; pc.Start(); richTextBox1.Text = pc.StandardOutput.ReadToEnd(); pc.WaitForExit(); } </code></pre> <p><strong>EDIT</strong></p> <p>with the your help (thanks a lot guys) and this link: <a href="http://msdn.microsoft.com/query/dev10.query?appId=Dev10IDEF1&amp;l=EN-US&amp;k=k%28EHINVALIDOPERATION.WINFORMS.ILLEGALCROSSTHREADCALL%29;k%28TargetFrameworkMoniker-%22.NETFRAMEWORK%2cVERSION%3dV2.0%22%29;k%28DevLang-CSHARP%29&amp;rd=true" rel="nofollow noreferrer">http://msdn.microsoft.com/query/dev10.query?appId=Dev10IDEF1&amp;l=EN-US&amp;k=k%28EHINVALIDOPERATION.WINFORMS.ILLEGALCROSSTHREADCALL%29;k%28TargetFrameworkMoniker-%22.NETFRAMEWORK%2cVERSION%3dV2.0%22%29;k%28DevLang-CSHARP%29&amp;rd=true</a></p> <p>I solve it with this code (do you think it is OK?):</p> <pre><code>namespace GUIforCL2 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } Process _cmd; delegate void SetTextCallback(string text); private void SetText(string text) { if (this.richTextBox1.InvokeRequired) { SetTextCallback d = new SetTextCallback(SetText); this.Invoke(d, new object[] { text }); } else { this.richTextBox1.Text += text + Environment.NewLine; } } private void button1_Click(object sender, EventArgs e) { ProcessStartInfo cmdStartInfo = new ProcessStartInfo("tracert.exe"); cmdStartInfo.Arguments = "google.com"; cmdStartInfo.CreateNoWindow = true; cmdStartInfo.RedirectStandardInput = true; cmdStartInfo.RedirectStandardOutput = true; cmdStartInfo.RedirectStandardError = true; cmdStartInfo.UseShellExecute = false; cmdStartInfo.WindowStyle = ProcessWindowStyle.Hidden; _cmd = new Process(); _cmd.StartInfo = cmdStartInfo; if (_cmd.Start()) { _cmd.OutputDataReceived += new DataReceivedEventHandler(_cmd_OutputDataReceived); _cmd.ErrorDataReceived += new DataReceivedEventHandler(_cmd_ErrorDataReceived); _cmd.Exited += new EventHandler(_cmd_Exited); _cmd.BeginOutputReadLine(); _cmd.BeginErrorReadLine(); } else { _cmd = null; } } void _cmd_OutputDataReceived(object sender, DataReceivedEventArgs e) { UpdateConsole(e.Data); } void _cmd_ErrorDataReceived(object sender, DataReceivedEventArgs e) { UpdateConsole(e.Data, Brushes.Red); } void _cmd_Exited(object sender, EventArgs e) { _cmd.OutputDataReceived -= new DataReceivedEventHandler(_cmd_OutputDataReceived); _cmd.Exited -= new EventHandler(_cmd_Exited); } private void UpdateConsole(string text) { UpdateConsole(text, null); } private void UpdateConsole(string text, Brush color) { WriteLine(text, color); } private void WriteLine(string text, Brush color) { if (text != null) { SetText(text); } } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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