C#双向IPC over stdin和stdout

时间:2009-05-28 17:40:56

标签: c# ipc stdout stdin

如何连接两个C#进程,以便它们可以通过stdin和stdout相互通信?

像这样:

过程A - > stdout A - > stdin B --->进程B

过程A< - stdin A< - stdout B< ---过程B

1 个答案:

答案 0 :(得分:4)

using System;
using System.Diagnostics;

class Program
{
  static void Main(string[] args)
  {
    string name;
    if (args.Length > 0 && args[0] == "slave")
    {
      name = "slave";
    }
    else
    {
      name = "master";
      var info = new ProcessStartInfo();
      info.FileName = "BidirConsole.exe";
      info.Arguments = "slave";
      info.RedirectStandardInput = true;
      info.RedirectStandardOutput = true;
      info.UseShellExecute = false;
      var other = Process.Start(info);
      Console.SetIn(other.StandardOutput);
      Console.SetOut(other.StandardInput);
    }
    Console.WriteLine(name + " started.");
    while (true)
    {
      var incoming = Console.ReadLine();
      var outgoing = name + " got : " + incoming;
      Console.WriteLine(outgoing);
      System.Threading.Thread.Sleep(100);
    }
  }
}