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

发布于 2024-08-12 10:30:58 字数 1431 浏览 11 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

颜漓半夏 2024-08-19 10:30:58

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

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.

指尖上得阳光 2024-08-19 10:30:58

(更新)这是一个古老的答案,因此您今天很可能不想使用远程处理。 :) 如果您想要 .NET 框架解决方案,您仍然可以使用 WCF,但是使用开源库,例如 MessagePipe< /a> 也许是一个更好的主意,更容易设置并且速度更快。


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

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

(Update) This is an ancient answer, so most likely you won't want to use Remoting today. :) If you want a .NET framework solution, you can still use WCF, however using an open source library like MessagePipe is perhaps a better idea, easier to set up and much faster.


You can try .NET Remoting. Here is a simple example: 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 WCF around.

一笔一画续写前缘 2024-08-19 10:30:58

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

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

请参阅此处

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

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

See here.

谁人与我共长歌 2024-08-19 10:30:58

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

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

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).

桃气十足 2024-08-19 10:30:58

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

服务器端代码:

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

IpcClientServer.RemoteMessage.MessageReceived += IpcClientServer_MessageReceived;

事件侦听器:

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

客户端:

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

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

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

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.

爱,才寂寞 2024-08-19 10:30:58

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

让一个程序在套接字上侦听,并让另一个程序连接到第一个程序。在一行上以数字字符串的形式发送两个整数。

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

I'd say make them talk over a socket.

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

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.

夜夜流光相皎洁 2024-08-19 10:30:58

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

For completeness, you can also to use net.pipe and WCF.

独﹏钓一江月 2024-08-19 10:30:58

我不是专业人士,但使用 StreamInsight 看到观察者设计模式似乎是最理想的采取的途径,但它在多线程应用程序中的重负载方面有局限性(你会得到太多的上下文切换)。

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

I am not a professional, but seeing the observer design pattern with StreamInsight appears to be the most desirable avenue to take, but it has limitations with heavy loads in multithreaded applications (you'll get too many context switches).

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

梦断已成空 2024-08-19 10:30:58

如果您不希望一个程序从另一个程序获取那么多数据,则可以让一个程序在某处创建一个 .txt 文件,然后让另一个程序读取该文本文件。

它也有局限性,比如它们不能同时读/写,这需要一些尝试/捕获来修复。这不是最专业的方法,但它完全有效。

If it’s not that much data that you want one program to get from the other, you could just have one program create a .txt file somewhere and have the other program read the text file.

It has limitations as well, like they both can't read/write at the same time which would need to have some try/catching to fix. It’s not the most professional way, but it totally works.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文