views:

139

answers:

3

I have a C# form that has a text box that needs to constantly update with the output from an exe while the exe is still running. I know how to update it after the exe has finished but its the constant updating that i need.

+1  A: 

You have your process which starts the exe:

// Normal creation and initialization of process - additionally:
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.OutputDataReceived += ProcessOnOutputDataReceived;
process.Start();
process.BeginOutputReadLine();

and the handler:

private void ProcessOnOutputDataReceived( object sender, DataReceivedEventArgs args )
{
    // use args.Data
}

EDIT:
I forgot process.StartInfo.UseShellExecute = false; :/

EDIT:
It turned out, that the exe did not flush the output (see comments)

tanascius
This still seems to wait for the exe to finish before the event is called. I only hit my breakpoint in the event when I force the exe to stop and then the event is called over and over with the output. Any ideas on how to improve this?
Chiefy
You have to set process.StartInfo.UseShellExecute = false;
tanascius
I already had that as false :S so im now very stuck.
Chiefy
What exe is it? Can you change the code - try to flush its output.
tanascius
yes the flush worked, thankyou very much :)
Chiefy
A: 

One way to do this would be to use remoting - in other words, you would poll a well known method in the running executable to retrieve the value and update the value in your textbox based on this. Google .net remoting for some samples.

Alternatively (if you're using .NET3 and on), you could use WCF and something like Tcp or Peer 2 Peer bindings. Without knowing more about your architecture, there's not much more I could say on this.

Pete OHanlon
A: 

When you say exe are you talking about running a console application by using process start?

If so then you have to get the output of the console app, see this question.

This is sending input there is an example to read output.

The other option is to have the calling class hook up to an event from your windows form. It will call the event will will cause the form to update the text box.

David Basarab