Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 787693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T21:14:41+00:00 2026-05-14T21:14:41+00:00

Is it possible to run any Command Line based program or batch file and

  • 0

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

the CL takes time and it produce text!

something like tracert.exe (it takes time and produce good amount of text).

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

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!

Simply I want to build is exactly like this http://www.codeproject.com/KB/threads/redir.aspx (check the demo) but in C#

here is my 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();    
}

EDIT

with the your help (thanks a lot guys) and this link:
http://msdn.microsoft.com/query/dev10.query?appId=Dev10IDEF1&l=EN-US&k=k%28EHINVALIDOPERATION.WINFORMS.ILLEGALCROSSTHREADCALL%29;k%28TargetFrameworkMoniker-%22.NETFRAMEWORK%2cVERSION%3dV2.0%22%29;k%28DevLang-CSHARP%29&rd=true

I solve it with this code (do you think it is OK?):

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);
            }
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T21:14:41+00:00Added an answer on May 14, 2026 at 9:14 pm

    When you create the Process instance for tracert, you need to set the ProcessStartInfo.UseShellExecute to false and ProcessStartInfo.RedirectStandardOutput to true. This will allow you to use Process.StandardOutput property to read the output.

    You can use Process.BeginOutputReadLine to start asynchronous reading and add an event handler to Process.OutputDataReceived.

    Update: Here’s an example WPF program that does what you want.

    MainWindow.xaml:

    <Window x:Class="Console.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded" Closed="Window_Closed">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="*" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>
            <ScrollViewer Name="outputViewer" SizeChanged="ScrollViewer_SizeChanged" >
                <TextBlock Name="output" />
            </ScrollViewer>
            <TextBox Grid.Row="1" Name="input" KeyDown="input_KeyDown" />
        </Grid>
    </Window>
    

    MainWindow.cs:

    using System;
    using System.Diagnostics;
    using System.Windows;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    
    namespace Console
    {
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
            }
    
            private void Window_Loaded(object sender, RoutedEventArgs e)
            {
                ProcessStartInfo cmdStartInfo = new ProcessStartInfo("cmd.exe");
                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() == true)
                {
                    _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;
                }
            }
    
            private void Window_Closed(object sender, EventArgs e)
            {
                if ((_cmd != null) &&
                    (_cmd.HasExited != true))
                {
                    _cmd.CancelErrorRead();
                    _cmd.CancelOutputRead();
                    _cmd.Close();
                    _cmd.WaitForExit();
                }
            }
    
            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 ScrollViewer_SizeChanged(object sender, SizeChangedEventArgs e)
            {
                outputViewer.ScrollToBottom();
            }
    
            private void input_KeyDown(object sender, KeyEventArgs e)
            {
                switch (e.Key)
                {
                    case Key.Enter:
                        _cmd.StandardInput.WriteLine(input.Text);
                        input.Text = "";
                        break;
                    case Key.Escape:
                        input.Text = "";
                        break;
                }
            }
    
            private void UpdateConsole(string text)
            {
                UpdateConsole(text, null);
            }
    
            private void UpdateConsole(string text, Brush color)
            {
                if (!output.Dispatcher.CheckAccess())
                {
                    output.Dispatcher.Invoke(
                            new Action(
                                    () =>
                                    {
                                        WriteLine(text, color);
                                    }
                                )
                        );
                }
                else
                {
                    WriteLine(text, color);
                }
            }
    
            private void WriteLine(string text, Brush color)
            {
                if (text != null)
                {
                    Span line = new Span();
                    if (color != null)
                    {
                        line.Foreground = color;
                    }
                    foreach (string textLine in text.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries))
                    {
                        line.Inlines.Add(new Run(textLine));
                    }
                    line.Inlines.Add(new LineBreak());
                    output.Inlines.Add(line);
                }
            }
    
            Process _cmd;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 405k
  • Answers 405k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer What if you do a SELECT * FROM YourTable WITH(NOLOCK)… May 15, 2026 at 5:47 am
  • Editorial Team
    Editorial Team added an answer If you want the user to see the resulting page,… May 15, 2026 at 5:47 am
  • Editorial Team
    Editorial Team added an answer I believe it will be created in current working directory… May 15, 2026 at 5:47 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.