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

前端 未结 8 1602
礼貌的吻别
礼貌的吻别 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.

提交回复
热议问题