1

Im using Stockfish game engine to power Human Vs Computer games. Here is first part of the code:

Process _proc= new Process();
_proc.StartInfo = new ProcessStartInfo(path);
_proc.StartInfo.RedirectStandardInput = true;
_proc.StartInfo.RedirectStandardOutput = true;
_proc.StartInfo.UseShellExecute = false;
_proc.StartInfo.CreateNoWindow = true;
_proc.Start();
_proc.StandardInput.WriteLine("uci");
_proc.StandardInput.WriteLine("ucinewgame");

At this point everything is ok, but when I try to read StandardOutput something weird happens.

string result = _proc.StandardOutput.ReadToEnd();

Stockfish.exe program pops-up my application is running but code after that line is not executing. When I press pause, it points at this line:

enter image description here

If I use:

while (!_proc.StandardOutput.EndOfStream)
{
    result += _proc.StandardOutput.ReadLine();
}

Same thing happens only at while statement. result has its full value there, all the text is written into it.

Is there any way to overcome this without async reading?

Side problem:

Since this is all part of singleton class that is used over whole ASP.NET application, i dont feel like using async reading since Im not sure how can I protect (with locking) multiple threads writing into it. Also, I dont know how to stop current thread since the processing of command can last up to 10 sec.

I don't feel like using Thread.Sleep() to constantly check for end of reading output, not elegant.

Considering side problem, how could i avoid multithread problems if async is only solution?

My threading knowledge is weak, so please have that in mind when giving thread related answers. Thank you.

4

1 に答える 1

0

StandardOutput.ReadToEndの呼び出しは、このプロセスが終了するまでブロックされます。ここでの目標は、生成したプロセスから受け取ったさまざまなテキストコマンドを読み取り、処理し、応答することですか?
非同期読み取りを介してこれにアプローチする必要があります。

たとえば、Process.OutputDataReceivedにリスナーを設定できます。次に、Process.BeginOutputReadLineを呼び出して読み取りを開始します。コードは実行を継続します。一方、.NET Frameworkは、着信テキストメッセージを別のスレッドで処理します。

于 2012-09-24T18:01:27.523 に答える