16

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

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

4

9 回答 9

21

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

于 2009-11-26T09:27:24.203 回答
4

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

如果您使用的是 .NET 3.5,则应该选择 WCF,因为 Remoting 正在慢慢过时。同样,有很多关于 WCF 的示例

于 2009-11-26T10:17:51.237 回答
2

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

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

这里

于 2009-12-13T20:34:29.387 回答
1

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

于 2009-11-26T09:23:41.997 回答
1

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

于 2009-11-26T09:24:22.353 回答
1

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

服务器端代码:



       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 的引用。

于 2019-01-05T12:29:01.060 回答
0

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.

于 2009-11-26T09:19:11.597 回答
0

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

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

于 2019-03-24T11:59:49.713 回答
0

如果您希望一个程序从另一个程序获取的数据不多,您可以让一个程序在某处创建一个 .txt 文件,并让另一个程序读取该文本文件。它也有局限性,就像它们不能同时读/写,这需要一些尝试/捕获来修复。不是最专业的方式,但它完全有效。

于 2020-12-28T20:23:19.403 回答