17

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

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
genesys
  • 191
  • 1
  • 2
  • 5

9 Answers9

21

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.

Greg Beech
  • 128,213
  • 43
  • 201
  • 246
4

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.

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Groo
  • 47,804
  • 16
  • 116
  • 191
2

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.

Bart
  • 18,962
  • 7
  • 68
  • 76
TheCodeKing
  • 18,816
  • 3
  • 46
  • 70
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).

Bobby
  • 11,167
  • 5
  • 43
  • 68
  • 2
    I strongly disagree with the clipboard part. And I see no point in *imitating* a named pipe, when you can simply use actual named pipes? – Groo Nov 26 '09 at 10:10
1

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

StayOnTarget
  • 9,925
  • 10
  • 45
  • 68
Rubens Farias
  • 55,782
  • 8
  • 132
  • 160
1

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

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.

Jonas Kölker
  • 7,402
  • 2
  • 42
  • 50
  • Actually, as far as I remember, you can specify from which addresses to accept connections (or at least check from what address it is coming, and if it is not localhost...well, never talk to strangers ;) ). – Bobby Nov 26 '09 at 09:25
0

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

kent
  • 1
0

If its 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. Not the most professional way but it totally works.