Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Repeatably Feeding Input to a Process' Standard Input

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).

like image 290
Kasper Holdum Avatar asked Jul 17 '11 01:07

Kasper Holdum


1 Answers

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 :).

like image 114
Jalal Said Avatar answered Nov 10 '22 03:11

Jalal Said