18

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,738
  • 21
  • 105
  • 131
genesys
  • 201
  • 1
  • 3
  • 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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Greg Beech
  • 133,383
  • 43
  • 204
  • 250
5

(Update) This is an ancient answer, so most likely you won't want to use Remoting today. :) If you want a .NET framework solution, you can still use WCF, however using an open source library like MessagePipe is perhaps a better idea, easier to set up and much faster.


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.

vgru
  • 49,838
  • 16
  • 120
  • 201
  • 1
    The first link is still up, but the second link is *broken*. It times out: *"The connection has timed out. The server at www.switchonthecode.com is taking too long to respond."* – Peter Mortensen Nov 23 '22 at 22:53
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
  • 19,692
  • 7
  • 68
  • 77
TheCodeKing
  • 19,064
  • 3
  • 47
  • 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,419
  • 5
  • 44
  • 69
  • 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? – vgru Nov 26 '09 at 10:10
1

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
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,680
  • 3
  • 44
  • 51
  • 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

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

StayOnTarget
  • 11,743
  • 10
  • 52
  • 81
Rubens Farias
  • 57,174
  • 8
  • 131
  • 162
0

I am not a professional, but seeing the observer design pattern with StreamInsight appears to be the most desirable avenue to take, but it has limitations with heavy loads in multithreaded 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 it is easy to install too. The website is XDMessaging.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
kent
  • 1
0

If it’s 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. It’s not the most professional way, but it totally works.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131