Sign in to follow this  
50p

[C#] How to capture console output?

Recommended Posts

Hey guys, I've been researching for entire day and still can't find any solution to what I'm trying to make. As the title says, I'm trying to find a way to get console application's output into a C# application. Basically, I wanted to make an application which allows users to simply compile Lua scripts (so luac.exe is the console app). I can't seem to find any way to get the output from luac.exe. This is the code I can find on almost every website:
      Process compiler = new Process();
      ProcessStartInfo info = new ProcessStartInfo("luac.exe");
      info.Arguments = "-o 'c:/lua.luac' 'c:/lua.lua'";
      info.RedirectStandardOutput = true;
      info.UseShellExecute = false;
      info.CreateNoWindow = true;
      compiler.StartInfo = info;
      compiler.Start();

      StreamReader reader = compiler.StandardOutput;
      textBox1.Text = reader.ReadLine();

      compiler.Close();
But all it does is run the luac.exe and do nothing. When I tried to debug it, and placed breakpoint at line with StreamReader, the compiler.StandardOutput is empty... Screenshot of debug As you can see there are exceptions thrown but I'm not informed about them when the application runs. I have to make it in C# because it's simple and quick to make applications, besides I've been making lua editor and wanted to use the compiler from within the editor. I downloaded wxWidgets and it has an example app which does exactly what I want (this application is called exec). I don't know if I get nothing because the compiler is made in C/C++ and my app in C# .NET or something is wrong with my code or maybe the way I tried (the code above) is only supported with .NET console applications. Any help is appreciated! Thanks in advance. [Edited by - 50p on June 20, 2009 10:42:27 PM]

Share this post


Link to post
Share on other sites
I don't have any experience with this but try pausing between

compiler.Start();

and

StreamReader reader = compiler.StandardOutput;


Process compiler = new Process();

ProcessStartInfo info = new ProcessStartInfo("luac.exe");

info.Arguments = "-o 'c:/lua.luac' 'c:/lua.lua'";

info.RedirectStandardOutput = true;

info.UseShellExecute = false;

info.CreateNoWindow = true;

compiler.StartInfo = info;

compiler.Start();

Thread.Sleep( 1000 );

StreamReader reader = compiler.StandardOutput;

textBox1.Text = reader.ReadLine();



compiler.Close();


Share this post


Link to post
Share on other sites
Try with linking with StandardError, because lots of application use this channel to emit into the console. (And yes try with the suggestion of waiting till the process is completely runned)

Share this post


Link to post
Share on other sites
Thanks. StandardError did the job... I don't know why StandardError didn't work for the first time I tried. BTW, I don't freeze the app for a sec. because the output is immediate. I tried to compile a file which weigh ~40kb (over 1000 lines) and made an error somewhere at the end of the file to see if I'll get the result immediately and... I did get an error message at line 1050 (where I changed the script) straight away. Maybe my PC is too fast for simple lua "compilation". Maybe I'll do Sleep for 500ms just to make sure the output is redirected properly.

Thanks to both of you.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this