4

I have a problem redirecting standard output of an application. It seems like this is some kind of bug in .NET.

I'm running Live555ProxyServer but I don't get any output even when console which starts does have a written output. This code works with any other console application but not with this one.

void StartProcess()
{
    var process = new Process();
    process.StartInfo.FileName = @"live555ProxyServer.exe";
    process.StartInfo.Arguments = "-R";
    process.StartInfo.UseShellExecute = false;
    process.StartInfo.RedirectStandardOutput = true;
    process.OutputDataReceived += process_OutputDataReceived;

    process.Start();
    process.BeginOutputReadLine();
}

void process_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
    Debug.WriteLine(e.Data);
}

The source code of that application can be found here

huysentruitw
  • 25,048
  • 8
  • 76
  • 120
Teamol
  • 409
  • 1
  • 9
  • 31
  • Have you checked that the process actually starts? process.Start() returns a boolean which should be true if the process started. – James Lucas Apr 30 '15 at 12:26
  • Yes it does start you can see that process and you can also see that it does have an output written in the console. – Teamol Apr 30 '15 at 12:28

1 Answers1

9

That is because all output goes to stderr instead of stdout, see source code

You should add a handler for Process.ErrorDataReceived and call Process.BeginErrorReadLine and things will start going smoothly.

huysentruitw
  • 25,048
  • 8
  • 76
  • 120
  • Thank you very much for pointing that out. The problem now is that ErrorDataRecieved is fired only after I close appliacation. Do you have any idea why this is happening? – Teamol Apr 30 '15 at 12:39
  • 1
    Probably because newlines are missing: http://stackoverflow.com/questions/1716296/why-does-printf-not-flush-after-the-call-unless-a-newline-is-in-the-format-strin You can change the code and add `fprintf(stderr, "\n");` or `fflush(stderr);` after each `fprintf` call. – huysentruitw Apr 30 '15 at 12:43
  • Strange thing in that answer is that they say `stderr` is unbuffered which seems not to be the case here. – huysentruitw Apr 30 '15 at 12:54