【问题标题】:C# bi-directional IPC over stdin and stdout标准输入和标准输出上的 C# 双向 IPC
【发布时间】:2010-10-29 15:56:36
【问题描述】:

如何连接两个 C# 进程,以便它们可以通过标准输入和标准输出相互通信?

像这样:

进程 A --> 标准输出 A --> 标准输入 B ---> 进程 B

进程 A

【问题讨论】:

    标签: c# ipc stdout stdin


    【解决方案1】:
    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);
        }
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-31
      • 1970-01-01
      • 2021-10-20
      • 2013-09-18
      • 2012-02-23
      • 2013-06-13
      相关资源
      最近更新 更多