Link to home
Start Free TrialLog in
Avatar of themaniac
themaniac

asked on

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
Avatar of Jens Fiederer
Jens Fiederer
Flag of United States of America image

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).
Avatar of themaniac
themaniac

ASKER

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.

Sounds like a job for a pipe!
Take a look at the docs for "anonymous pipes"
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();
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.
Great, but how do I know when to do the Readline, if the console application outputs some text from time to time?
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.
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
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

ASKER CERTIFIED SOLUTION
Avatar of Jens Fiederer
Jens Fiederer
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you very much both of you.You helped me a lot.