如何等待一个任务结束后再关闭进程?

问题描述 投票:0回答:1

最近我在做一个国际象棋,人工智能用的是Stockfish的UCI协议,感谢以下2个链接(UCI协议。http:/wbec-ridderkerk.nlhtmlUCIProtocol.html)。 和这篇文章 。在Unity中使用Stockfish棋牌AI 我已经取得了一些进展,但我有一个问题。

这是我的代码。

        var p = new System.Diagnostics.Process();

        p.StartInfo.FileName = "C:/stockfish/stockfish-11-win/Windows/stockfish_20011801_32bit";

        p.StartInfo.UseShellExecute = false;
        p.StartInfo.RedirectStandardInput = true;
        p.StartInfo.RedirectStandardOutput = true;

        p.Start();



        StreamWriter myStreamWriter = p.StandardInput;
        myStreamWriter.WriteLine("position fen " + fen);
        myStreamWriter.WriteLine("go movetime 2000");
        myStreamWriter.Close();

        Thread.Sleep(2000);



        StreamReader reader = p.StandardOutput;
        String bestMoveInAlgebraicNotation = reader.ReadToEnd();
        reader.Close();


        p.Close();

        return bestMoveInAlgebraicNotation;

当我用下面的代码要求Stockfish进行评估时。myStreamWriter.WriteLine("go movetime 2000");

他做了,但是当我要求进程关闭时,他马上就停止了。p.Close();

所以我想让processus等到任务完成,有什么方法可以做到吗?

感谢您阅读我的文章,祝您有一个愉快的一天。

c# unity3d chess uci
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.