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
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
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.
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.UseShellExecut e = false;
p.StartInfo.RedirectStanda rdOutput = 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();
From MSDN docs:
// Start the child process.
Process p = new Process();
// Redirect the output stream of the child process.
p.StartInfo.UseShellExecut
p.StartInfo.RedirectStanda
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.
ASKER
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.
You can do a "BeginRead" on the stream and specify a callback function to call when you get some text.
ASKER
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
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.
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);
}
}
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you very much both of you.You helped me a lot.
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).