views:

371

answers:

8

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  A: 

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
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
A: 

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
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
A: 

Mutex can be an option; please read this: Inter-Process Data Exchange using .NET 2.0

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

Rubens Farias
Mutexes are used for synchronization, not data transfer.
Groo
removed that Groo, ty
Rubens Farias
+2  A: 

See Use Microsoft Message Queuing in C# for inter-process communication

Alex
That fits my needs pretty well and is quite easy to use - thank you!
genesys
+5  A: 

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
A: 

Will MSMQ help??

atulchaudhari
+3  A: 

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.

Groo
A: 

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.

TheCodeKing