-1

2 つの異なる .exe ファイルを実行し、その結果をテキスト ファイルに出力するプログラムを作成しようとしています。別々に実行すると正常に動作しますが、両方を実行しようとすると、2 番目のプロセスが実行されません。誰でも助けることができますか?

これがコードです。Player1.exe と Player2.exe は、0 または 1 を返すコンソール アプリケーションです。

    static void Main(string[] args)
    {
        Process process1 = new Process();
        process1.StartInfo.FileName = "C:/Programming/Tournament/Player1.exe";
        process1.StartInfo.Arguments = "";
        process1.StartInfo.UseShellExecute = false;
        process1.StartInfo.RedirectStandardOutput = true;
        process1.Start();
        var result1 = process1.StandardOutput.ReadToEnd();
        process1.Close();

        Process process2 = new Process();
        process2.StartInfo.FileName = "C:/Programming/Tournament/Player2.exe";
        process2.StartInfo.Arguments = "";
        process2.StartInfo.UseShellExecute = false;
        process2.StartInfo.RedirectStandardOutput = true;
        process2.Start();
        string result2 = process2.StandardOutput.ReadToEnd().ToString();
        process2.Close();

        string resultsPath = "C:/Programming/Tournament/Results/Player1vsPlayer2.txt";
        if (!File.Exists(resultsPath))
        {
            StreamWriter sw = File.CreateText(resultsPath);
            sw.WriteLine("Player1 " + "Player2");
            sw.WriteLine(result1 + " " + result2);
            sw.Flush();
        }


    }
4

2 に答える 2

0

プロセスの使用についてはよくわかりませんが、別々のものを一度に実行する必要がある場合は、このアプローチを使用します。私はあなたのプロジェクトの一番下の部分を取り込んでいませんでしたが、これをチェックして、何らかの形で役立つかどうかを確認してください.

class Program
{
    static void Main(string[] args)
    {
        Process process1 = new Process();
        process1.StartInfo.FileName = "C:/Programming/Tournament/Player1.exe";
        process1.StartInfo.Arguments = "";
        process1.StartInfo.UseShellExecute = false;
        process1.StartInfo.RedirectStandardOutput = true;

        Process process2 = new Process();
        process2.StartInfo.FileName = "C:/Programming/Tournament/Player2.exe";
        process2.StartInfo.Arguments = "";
        process2.StartInfo.UseShellExecute = false;
        process2.StartInfo.RedirectStandardOutput = true;


        Thread T1 = new Thread(delegate() {
            doProcess(process1);
        });

        Thread T2 = new Thread(delegate() {
            doProcess(process2);
        });

        T1.Start();
        T2.Start();
    }

    public static void doProcess(Process myProcess)
    {
        myProcess.Start();
        var result1 = myProcess.StandardOutput.ReadToEnd();
        myProcess.Close();
    }
}
于 2013-09-19T19:52:27.377 に答える