1

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.

David Basarab
  • 72,212
  • 42
  • 129
  • 156
Chiefy
  • 179
  • 2
  • 17
  • 1
    Are you running the Exe with System.Diagnostics.Process ? – C. Ross Sep 04 '09 at 12:28
  • yes i am, I just want to get constant updates from the exe's output but everything ive tried seems to have to wait for the exe to end – Chiefy Sep 04 '09 at 12:58

3 Answers3

2

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
  • 53,078
  • 22
  • 114
  • 136
  • 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 Sep 04 '09 at 12:51
  • You have to set process.StartInfo.UseShellExecute = false; – tanascius Sep 04 '09 at 13:10
  • What exe is it? Can you change the code - try to flush its output. – tanascius Sep 04 '09 at 14:18
0

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
  • 9,086
  • 2
  • 29
  • 28
0

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.

Community
  • 1
  • 1
David Basarab
  • 72,212
  • 42
  • 129
  • 156