Output reader getting stuck for no reason
1. I did try without trying to read output or trying to read only first line of the output - both of these were successful.
2. However when I try to display full output, it just gets stuck and does nothing.
3. Even more crazy stuff - when I close the application, I am able to see some output for a split second.
4. I tried printing line by line and it gets stuck on the last line, however no bug or error is generated. It is just sitting there and that's it.
What is going on? :( It was quite difficult for me to find out how to interact with the program and now that I am successful (because everything is working), it just won't work for some reason.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;
using System.Threading;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
ProcessStartInfo processStartInfo = new ProcessStartInfo("Rybka32.exe", "");
processStartInfo.UseShellExecute = false;
processStartInfo.ErrorDialog = false;
processStartInfo.RedirectStandardError = true;
processStartInfo.RedirectStandardInput = true;
processStartInfo.RedirectStandardOutput = true;
Process process = new Process();
process.StartInfo = processStartInfo;
bool processStarted = process.Start();
StreamWriter inputWriter = process.StandardInput;
StreamReader outputReader = process.StandardOutput;
StreamReader errorReader = process.StandardError;
List<String> commands = new List<String>();
commands.Add("uci");
commands.Add("setoption name Hash value 32");
commands.Add("setoption name NalimovCache value 1");
commands.Add("setoption name NalimovPath value d:\tb;c\tb");
commands.Add("isready");
commands.Add("ucinewgame");
commands.Add("setoption name UCI_AnalyseMode value true");
commands.Add("position startpos moves e2e4 g7g5 d2d4 f7f6");
foreach (String command in commands)
{
System.Console.WriteLine("==>:" + command);
inputWriter.WriteLine(command);
string result = outputReader.ReadToEnd();
System.Console.WriteLine(result);
System.Console.WriteLine("This message is not written for some reason and the program seems to be stuck. *cries*");
}
System.Console.ReadKey();
}
}
}
Related Article: How to work with engines
is a solved C# discussion thread by kolibrizas that has 3 replies, was last updated 1 year ago and has been tagged with the keywords: chess, engine, input, output.
kolibrizas
Junior Poster in Training
52 posts since May 2009
Reputation Points: 22
Solved Threads: 2
Skill Endorsements: 0
You are attempting to control the Rybka32.exe by writing to it in an interactive fashion?
If you remove the last command does it still get stuck?
thines01
Postaholic
2,433 posts since Oct 2009
Reputation Points: 447
Solved Threads: 408
Skill Endorsements: 7
You are attempting to control the Rybka32.exe by writing to it in an interactive fashion?
If you remove the last command does it still get stuck?
Yes, I am trying to create UI and apply Rybka32.exe to it.
It gets stuck after the first command which I ask the output for - thus it only prints "==>:uci" and it's stuck. However, as I said, I have tried outputting only first lines and was a success for all commands :)
kolibrizas
Junior Poster in Training
52 posts since May 2009
Reputation Points: 22
Solved Threads: 2
Skill Endorsements: 0
If you look at "result" in the debugger, does it show anything?
thines01
Postaholic
2,433 posts since Oct 2009
Reputation Points: 447
Solved Threads: 408
Skill Endorsements: 7
kolibrizas
Junior Poster in Training
52 posts since May 2009
Reputation Points: 22
Solved Threads: 2
Skill Endorsements: 0
just as I do
string result = outputReader.ReadToEnd();
everything after it stops, I even cannot print it in debugger window. I do have it wrapped in try-catch, but it doesn't print out any errors.
kolibrizas
Junior Poster in Training
52 posts since May 2009
Reputation Points: 22
Solved Threads: 2
Skill Endorsements: 0
So, it puts nothing in the result, right?
thines01
Postaholic
2,433 posts since Oct 2009
Reputation Points: 447
Solved Threads: 408
Skill Endorsements: 7
It kinda puts something, but it is really weird as it just stops program and kinda waits for something. As I said above, once I hit close button on application, I have a very short amount of time to see the output actually showing up. So to your question - the answer is no. It does fill result, and it even does it successfully, it's just that it waits for next Christmas without me asking for that.
kolibrizas
Junior Poster in Training
52 posts since May 2009
Reputation Points: 22
Solved Threads: 2
Skill Endorsements: 0
This is a working example. Had to pay a bit for my friend to do it tho.
There was a need of asynchronous output.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;
using System.Threading;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
Process p = new Process();
p.StartInfo.FileName = "Rybka.exe";
p.StartInfo.ErrorDialog = false;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
// Hook up async stdout and stderr reading
p.ErrorDataReceived += ConsoleDataReceived;
p.OutputDataReceived += ConsoleDataReceived;
List<String> commands = new List<String>();
commands.Add("uci");
commands.Add("setoption name Hash value 32");
commands.Add("setoption name NalimovCache value 1");
commands.Add("setoption name NalimovPath value d:\tb;c\tb");
commands.Add("isready");
commands.Add("ucinewgame");
commands.Add("setoption name UCI_AnalyseMode value true");
commands.Add("position startpos moves e2e4 g7g5 d2d4 f7f6");
commands.Add("go infinite");
if (p.Start())
{
[B]// Begin async stdout and stderr reading[/B]
p.BeginOutputReadLine();
p.BeginErrorReadLine();
foreach (string command in commands)
{
p.StandardInput.WriteLine(command);
}
p.StandardInput.WriteLine("exit");
p.WaitForExit();
}
}
static void ConsoleDataReceived(object sender, DataReceivedEventArgs e)
{
if (e.Data != null)
Console.WriteLine(e.Data);
}
}
}
kolibrizas
Junior Poster in Training
52 posts since May 2009
Reputation Points: 22
Solved Threads: 2
Skill Endorsements: 0
Question Answered as of 1 Year Ago by
thines01
and
skatamatic