• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 5142
  • Last Modified:

capture console application output into textbox

I need to launch a console application from my c# application.
While the console application is running(and it outputs text to the console), my c# application should capture the output in realtime, and fill a textbox with it.
I can't upload a console application here, so please use one of your choice.
Thanks
0
themaniac
Asked:
themaniac
  • 8
  • 4
2 Solutions
 
Jens FiedererCommented:
When you say "output text to the console" are you specifying you want to run the console app to output to an ACTUAL console window rather than redirecting its output to a file?

You can run a console app and redirect its output to a file, then use your c# app to read the file  (there CAN be buffering issues here, though, depending on how often the console app flushes its output).
0
 
themaniacAuthor Commented:
The output should be redirected to the c# app,  but not displayed to the console(or better, do not display the console app while running). I want to do this without files.
Again:
1. the c# launches the console app
2. the console app DOES NOT display anything, it should run "hidden"
3. the console app should redirect its output to the c# app.

0
 
Jens FiedererCommented:
Sounds like a job for a pipe!
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Jens FiedererCommented:
Take a look at the docs for "anonymous pipes"
0
 
Jens FiedererCommented:
Even simpler, you can use the Process.StartInfo  to redirect the standard output.

From MSDN docs:

// Start the child process.
 Process p = new Process();
 // Redirect the output stream of the child process.
 p.StartInfo.UseShellExecute = false;
 p.StartInfo.RedirectStandardOutput = true;
 p.StartInfo.FileName = "Write500Lines.exe";
 p.Start();
 // Do not wait for the child process to exit before
 // reading to the end of its redirected stream.
 // p.WaitForExit();
 // Read the output stream first and then wait.
 string output = p.StandardOutput.ReadToEnd();
 p.WaitForExit();
0
 
Jens FiedererCommented:
In your case, of course, you sound like you'd rather to the occasional .ReadLine or even asynchronous stream I/O to send your text to your textbox, not a ReadToEnd.
0
 
themaniacAuthor Commented:
Great, but how do I know when to do the Readline, if the console application outputs some text from time to time?
0
 
Jens FiedererCommented:
That's what I meant with the asynchronous stream I/O.

You can do a "BeginRead" on the stream and specify a callback function to call when you get some text.
0
 
themaniacAuthor Commented:
Sounds nice. Could you please give me an example? I never used callbacks so I don't know how to use them for capturing the output, as you say
Thank you
0
 
Jens FiedererCommented:
Sure...let's say we have this as our console app...
Just generates a bunch of lines with random 0-10 second intervals between.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
 
namespace OutputGenerator
{
    class Program
    {
        static void Main(string[] args)
        {
            Random generator = new Random();
            int max = 10 + generator.Next(500);
            for (int i = 0; i < generator.Next(500); i++)
            {
                int wait = generator.Next(10);
                Console.WriteLine(i.ToString() + " out of " + max.ToString() + " items - " + wait.ToString());
                Thread.Sleep(1000 * wait);
            }
        }
    }
}

Open in new window

0
 
Jens FiedererCommented:
In our main app, we use "ProcessRead" as our callback
// Where we start the child process.
             p = new Process();
             // Redirect the output stream of the child process.
             p.StartInfo.UseShellExecute = false;
             p.StartInfo.RedirectStandardOutput = true;
             p.StartInfo.FileName = "c:/proj/OutputGenerator/bin/debug/OutputGenerator.exe";
             p.Start();
             p.StandardOutput.BaseStream.BeginRead(buffer, 0, 1, ProcessRead, null);
 
// our callback
        static byte[] buffer = new byte[1];
        static Process p;
        static void ProcessRead(IAsyncResult result)
        {
            p.StandardOutput.BaseStream.EndRead(result);
            // do something with the byte at buffer[0]
            p.StandardOutput.BaseStream.BeginRead(buffer, 0, 1, ProcessRead, null);
        }

Open in new window

0
 
Fernando SotoCommented:
Hi themaniac;

Here is a code snippet of how to open a command line process/console and send it commands and receive the results in a multiline text box.

Fernando

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;
 
namespace WindowsFormsApplication21
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        delegate void UpdateConsoleWindowDelegate(String msg);
        
        Process console = null;
        StreamWriter consoleStreamWriter = null;
 
        private void btnCreateConsole_Click(object sender, EventArgs e)
        {
            console = new Process();
            console.StartInfo.FileName = "cmd";
            console.StartInfo.Arguments = @"\K";
            console.StartInfo.UseShellExecute = false;
            console.StartInfo.CreateNoWindow = true;
            console.StartInfo.RedirectStandardInput = true;
            console.StartInfo.RedirectStandardOutput = true;
            // Set our event handler to asynchronously read the sort output.
            console.OutputDataReceived += new DataReceivedEventHandler(ConsoleOutputHandler);
            console.Start();
            // Use a stream writer to synchronously write the sort input.
            consoleStreamWriter = console.StandardInput;
            console.BeginOutputReadLine();
            btnCreateConsole.Enabled = false;
        }
 
        private void btnSend_Click(object sender, EventArgs e)
        {
            consoleStreamWriter.WriteLine(txtCommand.Text);
        }
 
        private void btnTerminate_Click(object sender, EventArgs e)
        {
            if (!console.HasExited)
            {
                console.Kill();
                console.Dispose();
                console = null;
                btnCreateConsole.Enabled = true;
            }
        }
 
        private void UpdateConsoleWindow(String message)
        {
            if (txtConsoleWindow.InvokeRequired)
            {
                UpdateConsoleWindowDelegate update = new UpdateConsoleWindowDelegate(UpdateConsoleWindow);
                txtConsoleWindow.Invoke(update, message);
            }
            else
            {
                txtConsoleWindow.AppendText(message);
            }
        }
 
        private void ConsoleOutputHandler(object sendingProcess,
            DataReceivedEventArgs recieved)
        {
            if( !String.IsNullOrEmpty(recieved.Data ))
            {                
                UpdateConsoleWindow(recieved.Data + "\r\nReady>");
            }
        }
 
    }
}

Open in new window

0
 
themaniacAuthor Commented:
Thank you very much both of you.You helped me a lot.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 8
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now