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

前端 未结 8 1511
礼貌的吻别
礼貌的吻别 2020-11-30 11:20

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

相关标签:
8条回答
  • 2020-11-30 11:43

    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.

    0 讨论(0)
  • 2020-11-30 11:48

    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

    0 讨论(0)
  • 2020-11-30 11:50

    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.

    0 讨论(0)
  • 2020-11-30 11:57

    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.

    0 讨论(0)
  • 2020-11-30 11:58

    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.

    0 讨论(0)
  • 2020-11-30 12:01

    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.

    0 讨论(0)
提交回复
热议问题