代码之家  ›  专栏  ›  技术社区  ›  Horcrux7

在2个C#进程之间进行进程间通信的最简单方法是什么?

  •  48
  • Horcrux7  · 技术社区  · 16 年前

    我希望父母和孩子之间的沟通过程都是用C#写的。它应该是异步的,事件驱动的。我不想在处理非常罕见的通信的每个进程中运行线程。

    最好的解决办法是什么?

    7 回复  |  直到 5 年前
        1
  •  41
  •   Uwe Keim Tomasz    5 年前

    Anonymous pipes .

    对BeginRead/BeginWrite和AsyncCallback使用异步操作。

        2
  •  16
  •   Uwe Keim Tomasz    5 年前

    如果您的进程在同一台计算机上,您可以简单地使用 斯特迪奥 .

    这是我的用法,网页截图:

    var jobProcess = new Process();
    
    jobProcess.StartInfo.FileName = Assembly.GetExecutingAssembly().Location;
    jobProcess.StartInfo.Arguments = "job";
    
    jobProcess.StartInfo.CreateNoWindow = false;
    jobProcess.StartInfo.UseShellExecute = false;
    
    jobProcess.StartInfo.RedirectStandardInput = true;
    jobProcess.StartInfo.RedirectStandardOutput = true;
    jobProcess.StartInfo.RedirectStandardError = true;
    
    // Just Console.WriteLine it.
    jobProcess.ErrorDataReceived += jp_ErrorDataReceived;
    
    jobProcess.Start();
    
    jobProcess.BeginErrorReadLine();
    
    try
    {
        jobProcess.StandardInput.WriteLine(url);
        var buf = new byte[int.Parse(jobProcess.StandardOutput.ReadLine())];
        jobProcess.StandardOutput.BaseStream.Read(buf, 0, buf.Length);
        return Deserz<Bitmap>(buf);
    }
    finally
    {
        if (jobProcess.HasExited == false)
            jobProcess.Kill();
    }
    

    检测主节点上的args

    static void Main(string[] args)
    {
        if (args.Length == 1 && args[0]=="job")
        {
            //because stdout has been used by send back, our logs should put to stderr
            Log.SetLogOutput(Console.Error); 
    
            try
            {
                var url = Console.ReadLine();
                var bmp = new WebPageShooterCr().Shoot(url);
                var buf = Serz(bmp);
                Console.WriteLine(buf.Length);
                System.Threading.Thread.Sleep(100);
                using (var o = Console.OpenStandardOutput())
                    o.Write(buf, 0, buf.Length);
            }
            catch (Exception ex)
            {
                Log.E("Err:" + ex.Message);
            }
        }
        //...
    }
    
        3
  •  7
  •   bobwienholt    16 年前

    我建议使用Windows通信基础:

    http://en.wikipedia.org/wiki/Windows_Communication_Foundation

    您可以来回传递对象,使用各种不同的协议。我建议使用二进制tcp协议。

        4
  •  5
  •   Amy B    16 年前
        5
  •  1
  •   Community kfsone    7 年前

    还有 COM .

    这里有一些技术细节,但我想说的是,它的优点是您可以调用可以定义的方法。

    MSDN提供C#COM互操作教程。请搜索,因为这些链接确实发生了变化。

    马上开始走吧 here ...

        6
  •  1
  •   Rowan    6 年前

    还有MSMQ(微软消息队列),它可以跨网络运行,也可以在本地计算机上运行。尽管有更好的沟通方式,但值得研究: https://msdn.microsoft.com/en-us/library/ms711472(v=vs.85).aspx

        7
  •  0
  •   cdiggins    6 年前

    这个 最简单的 当安全性不受关注并且给定了约束(同一台机器上的两个C进程)时,C中用于进程间通信的解决方案是远程处理API。现在远程处理是一种遗留技术(与不推荐使用的技术不同),不鼓励在新项目中使用,但它确实工作得很好,并且不需要太多的浮夸和环境来工作。

    有一个 excellent article on MSDN for using the class IpcChannel 从远程处理框架(信贷 Greg Beech for the find here )用于设置简单的远程服务器和客户端。

    我建议首先尝试这种方法,然后尝试将代码移植到WCF(Windows通信框架)。它有几个优点(更好的安全性,跨平台),但必然更复杂。幸运的是MSDN有一个 very good article for porting code from Remoting to WCF .

    如果你想马上潜入 WCF there is a great tutorial here .

    推荐文章