0

I'm writing small console app for comunicate with chess engine. I'm setting process class properly and I can set input, but I can not capture output. With this starting parameters startInfo.RedirectStandardInput = true; startInfo.RedirectStandardOutput = false; input is possible. When I'm setting startInfo.RedirectStandardOutput = true; only empty console window is starting, no input possible. I saw this topic Catch console input-output and it's marked as accepted but it is not working for me.

For now my Program (how here I can read last line of output):

    var position = File.ReadAllText("C:\\pos.txt");

    ProcessStartInfo startInfo = new ProcessStartInfo();
    startInfo.RedirectStandardInput = true;
    startInfo.RedirectStandardOutput = false;
    startInfo.UseShellExecute = false;
    startInfo.ErrorDialog = false;
    startInfo.FileName = "C:\\engine.exe";

    Process process = new Process();
    process.StartInfo = startInfo;
    process.Start();

    process.StandardInput.WriteLine("position startpos moves " + position);
    process.StandardInput.WriteLine("go");
    System.Threading.Thread.Sleep(500);
    process.StandardInput.WriteLine("stop");
Community
  • 1
  • 1
Piotr M
  • 499
  • 3
  • 9
  • 27

1 Answers1

0

I,ve just answered this in your previous question. Just add these lines after process.StandardInput.WriteLine("stop");

string lastLine = null;
while (!process.StandardOutput.EndOfStream) 
{
    lastLine = process.StandardOutput.ReadLine();
}

//do what you want here with lastLine;
Hamid Pourjam
  • 20,441
  • 9
  • 58
  • 74
  • I tried this, and I get `An unhandled exception of type 'System.InvalidOperationException' occurred in System.dll Additional information: StandardOut has not been redirected or the process hasn't started yet.` at while() line. – Piotr M Jun 14 '15 at 16:36
  • you should set `startInfo.RedirectStandardOutput` to true! – Hamid Pourjam Jun 14 '15 at 16:38
  • You should read my question before downvoting, then whole process will not start :| – Piotr M Jun 14 '15 at 16:39
  • I did not down vote your question. what is the error when you set `startInfo.RedirectStandardOutput` to true? – Hamid Pourjam Jun 14 '15 at 16:41
  • 1
    Ok, this is really strange - when I'm doing as you said - code is working properly but console is EMPTY (that's why I thought it is not working, on previous version I SAW output at console). – Piotr M Jun 14 '15 at 16:46