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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1802475/
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-06 20:58:44  来源:igfitidea点击:

What is the easiest way to do inter process communication in C#?

c#ipccommunicationinterprocess

提问by genesys

I have two C# applications and I want one of them send two integers to the other one (this doesn't have to be fast since it's invoked only once every few seconds).

我有两个 C# 应用程序,我希望其中一个向另一个发送两个整数(这不必很快,因为它每几秒只调用一次)。

What's the easiest way to do this? (It doesn't have to be the most elegant one.)

什么是最简单的方法来做到这一点?(它不一定是最优雅的。)

回答by Jonas K?lker

I'd say make them talk over a socket.

我会说让他们通过套接字交谈。

Have one program listenon a socket and have the other connectto the first. Send the two integers on a single line, as strings of digits.

将一个程序listen放在一个套接字上,另一个程序放在connect第一个上。在一行上发送两个整数,作为数字字符串。

The only question is how they should agree on port numbers, and how they know that they're talking to one another. They can agree on port numbers by you deciding they should always use port 12345 (say), and the dirty-hacky-solution for the second part is to just trust whomever you're talking with to be a nice guy.

唯一的问题是他们应该如何就端口号达成一致,以及他们如何知道他们正在相互交谈。他们可以通过您决定他们应该始终使用端口 12345(比如说)来就端口号达成一致,而第二部分的肮脏解决方案是相信与您交谈的任何人都是好人。

回答by Bobby

Another way would be to imitate a named pipe. Declare a file somewhere, and read from/write to it.

另一种方法是模仿命名管道。在某处声明一个文件,并从中读取/写入。

Or, if the programs get executed in sequence, you could try the clipboard...but that solution is ugly as hell and is buggy (sometimes .NET can't access the clipboard for no reason).

或者,如果程序按顺序执行,您可以尝试剪贴板……但该解决方案非常丑陋并且有问题(有时.NET 无法无缘无故地访问剪贴板)。

回答by Rubens Farias

For completeness, you can also to use net.pipeand WCF.

为了完整起见,您还可以使用net.pipeWCF

回答by Greg Beech

The easiest and most reliable way is almost certainly IpcChannel(a.k.a. Inter Process Communication Channel); that's what it's there for. You can get it up and running with a couple of lines of code and configuration.

最简单和最可靠的方法几乎肯定是IpcChannel(又名进程间通信通道);这就是它的用途。您可以使用几行代码和配置来启动并运行它。

回答by Groo

You can try .NET Remoting. Here is a simple example: CodeProject .NET Remoting.

您可以尝试 .NET 远程处理。这是一个简单的示例:CodeProject .NET Remoting

If you are using .NET 3.5, you should go for WCF, as Remoting is slowly becoming obsolete. Again, there are many examples for WCFaround.

如果您使用 .NET 3.5,您应该选择 WCF,因为远程处理正在慢慢过时。同样,周围有许多WCF 示例

回答by TheCodeKing

I've created my own open source library for quick and easy IPC which works between Windows services, console applications and Windows forms.

我已经创建了我自己的开源库,用于在 Windows 服务、控制台应用程序和 Windows 窗体之间工作的快速和简单的 IPC。

It has some advantages over existing IPC implementations and doesn't require any configuration.

它比现有的 IPC 实现有一些优势,并且不需要任何配置。

See here.

这里

回答by Petteri Kautonen

I created a simple class which uses IpcChannel class for the inter process communication. Here is a link to a Gist at GitHub.

我创建了一个简单的类,它使用 IpcChannel 类进行进程间通信。这是GitHub 上 Gist的链接。

The server side code:

服务器端代码:



       IpcClientServer ipcClientServer = new IpcClientServer();
       ipcClientServer.CreateServer("localhost", 9090);

       IpcClientServer.RemoteMessage.MessageReceived += IpcClientServer_MessageReceived;
    
    

The event listener:

事件监听器:


    private void IpcClientServer_MessageReceived(object sender, MessageReceivedEventArgs e)
    {
        if (InvokeRequired)
        {
            Invoke(new MethodInvoker(delegate { textBox2.Text += e.Message + 
            Environment.NewLine; }));
        }
        else
        {
            textBox2.Text += e.Message + Environment.NewLine;
        }
    }


The client:

客户端:



        if (ipcClientServer == null)
        {
            ipcClientServer = new IpcClientServer();
            ipcClientServer.CreateClient("localhost", 9090);
        }
        ipcClientServer.SendMessage(textBox1.Text);


Note: A reference to a System.Runtime.Remoting is required.

注意:需要对 System.Runtime.Remoting 的引用。

回答by kent

I am no pro, but seeing the observer design pattern with StreamInsight appears to be the most desirable avenue to take, but has limitations with heavy loads in multi threaded applications (you'll get too many context switches).

我不是专业人士,但通过 StreamInsight 看到观察者设计模式似乎是最理想的途径,但在多线程应用程序中存在重负载的限制(您将获得太多上下文切换)。

As an amateur programmer, I have found XDMessaging to be easy and simple for me. It is in NuGet so easy to install too, website is XDMessaging

作为一名业余程序员,我发现 XDMessaging 对我来说很简单。它在 NuGet 中也很容易安装,网站是XDMessaging