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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/528652/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 06:53:35  来源:igfitidea点击:

What is the simplest method of inter-process communication between 2 C# processes?

c#ipc

提问by Horcrux7

I want communicate between a parent and child process both written in C#. It should be asynchronous, event driven. I does not want run a thread in every process that handle the very rare communication.

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

What is the best solution for it?

什么是最好的解决方案?

回答by Carlos A. Ibarra

Anonymous pipes.

匿名管道

Use Asynchronous operations with BeginRead/BeginWrite and AsyncCallback.

将异步操作与 BeginRead/BeginWrite 和 AsyncCallback 一起使用。

回答by bobwienholt

I would suggest using the Windows Communication Foundation:

我建议使用 Windows Communication Foundation:

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

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

You can pass objects back and forth, use a variety of different protocols. I would suggest using the binary tcp protocol.

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

回答by IlPADlI

If your processes in same computer, you can simply use stdio.

如果您的进程在同一台计算机上,您可以简单地使用stdio

This is my usage, a web page screenshooter:

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

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();
}

Detect args on Main

检测 Main 上的参数

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);
        }
    }
    //...
}

回答by Kay Zed

There's also COM.

还有COM

There are technicalities, but I'd say the advantage is that you'll be able to call methods that you can define.

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

MSDN offers C# COM interop tutorials. Please search because these links do change.

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

To get started rightaway go here...

要立即开始,请访问这里...

回答by Rowan

There's also MSMQ (Microsoft Message Queueing) which can operate across networks as well as on a local computer. Although there are better ways to communicate it's worth looking into: https://msdn.microsoft.com/en-us/library/ms711472(v=vs.85).aspx

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

回答by cdiggins

The easiestsolution in C# for inter-process communication when security is not a concern and given your constraints (two C# processes on the same machine) is the Remoting API. Now Remoting is a legacy technology (not the same as deprecated) and not encouraged for use in new projects, but it does work well and does not require a lot of pomp and circumstance to get working.

最简单的C#解决方案的进程间通信时的安全性是不是一个问题并给予你的约束(在同一台机器上的两个C#程序)是Remoting的API。现在 Remoting 是一项遗留技术(与已弃用的技术不同),不鼓励在新项目中使用,但它确实运行良好,并且不需要太多的排场和环境即可开始工作。

There is an excellent article on MSDN for using the class IpcChannelfrom the Remoting framework (credit to Greg Beech for the find here) for setting up a simple remoting server and client.

MSDN 上IpcChannel有一篇出色的文章,介绍了使用Remoting 框架中的类感谢 Greg Beech 在这里找到)来设置简单的远程处理服务器和客户端。

I Would suggest trying this approach first, and then try to port your code to WCF (Windows Communication Framework). Which has several advantages (better security, cross-platform), but is necessarily more complex. Luckily MSDN has a very good article for porting code from Remoting to WCF.

我建议先尝试这种方法,然后尝试将您的代码移植到 WCF(Windows 通信框架)。这有几个优点(更好的安全性、跨平台),但必然更复杂。幸运的是,MSDN 有一篇非常好的文章将代码从 Remoting 移植到 WCF

If you want to dive in right away with WCF there is a great tutorial here.

如果您想立即深入了解WCF,这里有一个很棒的教程