7

I have a (C#) console application which maintains a state. The state can be altered by feeding the application with various input through the console. I need to be able to both feed the application with a bit of input, then read the output rinse and repeat.

I create a new process and do all of the normal work of redirecting the input/output. The problem is that after I've sent input and call ReadLine() on the standard output it does not return a value before I call Close() on the standard input after which I cannot write anymore to the input stream.

How can I keep open the input stream while still receiving output?

 var process = new Process
                          {
                              StartInfo =
                                  {
                                      FileName =
                                          @"blabal.exe",
                                      RedirectStandardInput = true,
                                      RedirectStandardError = true,
                                      RedirectStandardOutput = true,
                                      UseShellExecute = false,
                                      CreateNoWindow = true,
                                      ErrorDialog = false
                                  }
                          };


        process.EnableRaisingEvents = false;

        process.Start();

        var standardInput = process.StandardInput;
        standardInput.AutoFlush = true;
        var standardOutput = process.StandardOutput;
        var standardError = process.StandardError;

        standardInput.Write("ready");
        standardInput.Close(); // <-- output doesn't arrive before after this line
        var outputData = standardOutput.ReadLine();

        process.Close();
        process.Dispose();

The console application I'm redirecting IO from is very simple. It reads from the console using Console.Read() and writes to it using Console.Write(). I know for certain that this data is readable, since I have another application that reads from it using standard output / input (not written in .NET).

Kasper Holdum
  • 12,993
  • 6
  • 45
  • 74

1 Answers1

4

That is happening because of you are using Write("ready") which is will append a string to the text, instead use WriteLine("ready"). that simple :).

Jalal Said
  • 15,906
  • 7
  • 45
  • 68
  • Noway! God, you are correct. What if I needed to write something that wasn't prepended with \r\n? Like prepending it linux style with a single '\n'. – Kasper Holdum Jul 17 '11 at 03:48
  • 3
    @Qua: Use `Environment.NewLine` instead, `A string containing "\r\n" for non-Unix platforms, or a string containing "\n" for Unix platforms.` check [msdn](http://msdn.microsoft.com/en-us/library/system.environment.newline.aspx). – Jalal Said Jul 17 '11 at 03:56
  • WriteLine will automatically use Enviroment.Newline then? – Kasper Holdum Jul 17 '11 at 04:18