Capture console and output in realtime
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Capture console and output in realtime

  1. #1
    Join Date
    Sep 2008
    Posts
    4

    Capture console and output in realtime

    Here's what I want to do:

    I want to open a console window with the push of a button and then capture the window output into my gui. This console window will be closed on user termination so I need the output capture to continuously work until program termination. I have tried the redirect standard output methods but it waits until the program exits and then shows the output.

    If anyone could help me, that would be great.

  2. #2
    Join Date
    Sep 2008
    Posts
    4

    Re: Capture console and output in realtime

    anyone?

  3. #3
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    210

    Re: Capture console and output in realtime

    the only way I know is, to start another console application and redirect its output to a variable, which can be displayed in the WinForm GUI.

    This is done by:


    System.Diagnostics.Process p = new System.Diagnostics.Process();
    p.StartInfo.FileName = fileName;
    p.StartInfo.Arguments = arguments;
    p.StartInfo.UseShellExecute = false;
    p.StartInfo.RedirectStandardOutput = true;
    p.StartInfo.CreateNoWindow = false; // true;


    p.Start();
    string sOutput = p.StandardOutput.ReadToEnd();

    p.Wait...();
    int exitCode = p.ExitCode;
    p.Close();

    //richTextBox_Output.Text = sOutput;
    return exitCode;
    Last edited by MNovy; September 12th, 2008 at 01:14 AM.

  4. #4
    Arjay's Avatar
    Arjay is online now Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    12,460

    Re: Capture console and output in realtime

    Handle the OutputDataReceived event.

    Code:
    process.StartInfo.RedirectStandardOutput = true;
    process.OutputDataReceived += new DataReceivedEventHandler( process_OutputDataReceived );
    
    process.Start( );
    
    process.WaitFor.Exit( timeout );
    
    void process_OutputDataReceived( object sender, DataReceivedEventArgs e )
    {
      // Std output arrives here
    }

  5. #5
    Join Date
    Sep 2008
    Posts
    4

    Re: Capture console and output in realtime

    Quote Originally Posted by Arjay
    Handle the OutputDataReceived event.

    Code:
    process.StartInfo.RedirectStandardOutput = true;
    process.OutputDataReceived += new DataReceivedEventHandler( process_OutputDataReceived );
    
    process.Start( );
    
    process.WaitFor.Exit( timeout );
    
    void process_OutputDataReceived( object sender, DataReceivedEventArgs e )
    {
      // Std output arrives here
    }
    I see that you use process.waitfor.exit(). Doesn't that mean it has to wait for the exit of the application. I do not want it to wait for the application to finish. I want it to continuously work until the user presses a button to terminate the program.

    This is more of what I wanted to do:
    Code:
    Public Class Form1
    
        Private WithEvents tb As New TextBox
    
        Sub New()
            InitializeComponent()
            Me.Text = "click the textbox"
            Me.Controls.Add(tb)
            tb.Dock = DockStyle.Fill
            tb.ReadOnly = True
            tb.ScrollBars = ScrollBars.Both
            tb.Multiline = True
            Me.Width = 500
        End Sub
    
        Sub Go()
            Dim p As New Process
            p.StartInfo.FileName = "ping.exe"
            p.StartInfo.Arguments = "www.bbc.co.uk"
            p.StartInfo.UseShellExecute = False
            p.StartInfo.RedirectStandardOutput = True
            AddHandler p.OutputDataReceived, AddressOf HelloMum
            p.Start()
            p.BeginOutputReadLine()
        End Sub
    
        Sub HelloMum(ByVal sender As Object, ByVal e As DataReceivedEventArgs)
            UpdateTextBox(e.Data)
        End Sub
    
        Private Delegate Sub UpdateTextBoxDelegate(ByVal Text As String)
        Private Sub UpdateTextBox(ByVal Tex As String)
            If Me.InvokeRequired Then
                Dim del As New UpdateTextBoxDelegate(AddressOf UpdateTextBox)
                Dim args As Object() = {Tex}
                Me.Invoke(del, args)
            Else
                tb.Text &= Tex & Environment.NewLine
            End If
        End Sub
    
        Private Sub tb_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles tb.Click
            Go()
        End Sub
    
    End Class
    But that's in VB.net. Could someone help me get it into C#?
    Thanks again for the help.

  6. #6
    Arjay's Avatar
    Arjay is online now Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    12,460

    Re: Capture console and output in realtime

    Quote Originally Posted by algrn912005
    I see that you use process.waitfor.exit(). Doesn't that mean it has to wait for the exit of the application.
    No, the OutputDataReceived handler will still get called. You can take the Wait*****it line out but you need to declare the process on the class level, not within a function.

    See the attached zip file for an example. I've created two apps: a winforms app (called CGStdOutput) that uses the process class to launch an app called CGDir. The CGDir app just grabs the system directory and writes the file names to the console window (i.e std output). The CGStdOutput launches the CGDir app and set an OutputDataReceived handler to put the std output (i.e. the filenames from the CGDir app) into a listbox.

    CGDir code:
    Code:
    static void Main( string[ ] args )
    {
      DirectoryInfo di = new DirectoryInfo( Environment.SystemDirectory );
      foreach( FileInfo fileInfo in di.GetFiles( ) )
      {
        Console.WriteLine( fileInfo.ToString( ) );
      }
    }
    CGStdOutput code:
    Code:
    public partial class Form1 : Form
    {
            public Form1( )
            {
                InitializeComponent( );
            }
    
            private void _btnRun_Click( object sender, EventArgs e )
            {
                _btnRun.Enabled = false;
    
                if( null != _process )
                {
                    _process.Dispose( );
                }
    
                _process = new Process( );
                _process.StartInfo.FileName = "CGDir.exe";
    
                _process.StartInfo.UseShellExecute = false;
    
                _process.EnableRaisingEvents = true;
                _process.StartInfo.CreateNoWindow = true;
                _process.StartInfo.RedirectStandardOutput = true;
    
                _process.OutputDataReceived += new DataReceivedEventHandler( OnOutputDataReceived );
                _process.Exited += new EventHandler( OnProcessExited );
    
                _process.Start( );
    
                _process.BeginOutputReadLine( );
            }
    
            /// <summary>
            /// Standard Output event handler - called when standard output text is available from
            /// the launched process.
            /// This event handler gets called from a different thread than the main UI thread.
            /// As such we need to use a delegate to access the UI thread.
            /// </summary>
            void OnOutputDataReceived( object sender, DataReceivedEventArgs e )
            {
                if( _listBox.InvokeRequired && !String.IsNullOrEmpty( e.Data ) )
                {
                    // We use an anonymous delegate here
                    _listBox.Invoke( new MethodInvoker( delegate( ) { _listBox.Items.Add( e.Data ); } ) );
                }
            }
    
            /// <summary>
            /// Called when process exits
            /// </summary>
            void OnProcessExited( object sender, EventArgs e )
            {
                if( _btnRun.InvokeRequired )
                {
                    // We use an anonymous delegate here
                    _btnRun.Invoke( new MethodInvoker( delegate( ) { _btnRun.Enabled = true; } ) );
                }
            }
    
            private Process _process = null;
    }
    Attached Files Attached Files

  7. #7
    Join Date
    Jan 2008
    Posts
    61

    Re: Capture console and output in realtime

    I don't know if this is exactly what you want but there is a way to redirect console output to a ricktextbox and it works at mine.
    Code:
    	public partial class MDIParent : Form
    	{
    		private StringRedir RedirConsole;
    		private TextWriter ConsoleWriter;
    
    		public MDIParent()
    		{
    			InitializeComponent();
    
    			// Here we redirect Console.WriteLine to a RichTextBox control. 
    			ConsoleWriter = Console.Out;	// Save the current console TextWriter. 
    			RedirConsole = new StringRedir(ref rtbOutput);
    			Console.SetOut(RedirConsole);	// Set console output to the StringRedir class. 
    		}
    
    		private void MDIParent_OnDestroy()
    		{
    			Console.SetOut(ConsoleWriter);	// Redirect Console back to original TextWriter. 
    			RedirConsole.Close();			// Close our StringRedir TextWriter. 
    		}
    	}
    
    	public class StringRedir : StringWriter
    	{ // Redirecting Console output to RichTextBox
    		private RichTextBox outBox;
    		delegate void Writeline(string x);
    
    		public StringRedir(ref RichTextBox textBox)
    		{
    			outBox = textBox;
    		}
    
    		public override void Write(string value)
    		{ // Centralize output in ONE function : this one
    			outBox.Text += value;
    			outBox.Refresh();
    		}
    
    		public override void WriteLine(string value)
    		{
    			Write(value + "\n");
    		}
    
    		public override void Write(string format, params object[] args)
    		{
    			Write(string.Format(format, args));
    		}
    
    		public override void WriteLine(string format, params object[] args)
    		{
    			Write(string.Format(format, args) + "\n");
    		}
    	}
    (BTW This code is not mine : I found it on the net because I looked for it myself some time ago)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This a Codeguru.com survey!


HTML5 Development Center