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

白昼怎懂夜的黑 提交于 2019-11-26 11:25:48

问题


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


回答1:


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.




回答2:


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.




回答3:


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.




回答4:


I created a simple class which uses IpcChannel class for the inter process communication. Here is a link to 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.




回答5:


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.




回答6:


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




回答7:


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




回答8:


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

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



来源:https://stackoverflow.com/questions/1802475/what-is-the-easiest-way-to-do-inter-process-communication-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!