Reputation: 201
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.)
Upvotes: 18
Views: 33113
Reputation: 1
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. here is github of XDMessaging.
Upvotes: 0
Reputation: 85
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.
Upvotes: 0
Reputation: 59
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.
Upvotes: 1
Reputation: 51282
(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.
Upvotes: 6
Reputation: 136717
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.
Upvotes: 21
Reputation: 19230
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.
Upvotes: 2
Reputation: 11576
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).
Upvotes: 1
Reputation: 7837
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.
Upvotes: 0