Reputation: 109
What is the best way to implement interprocess communication between applications that are on the same box -- both written in c#?
The manager application will be sending commands such as: stop, start to the other applications. It will also be monitoring the applications and possibly asking for data.
All applications will be on the same machine running on windows 7 OS.
Is IPC Channel a good choice for this? Is named Pipes a better choice? Or is sockets a better choice?
The applications that are being managed all have the same name. When they start up, they load in a dll that determines which algorithms are run.
Thanks
Upvotes: 4
Views: 1599
Reputation: 19394
One way to do it, especially if you want to communicate between different processes spun off the same application - using MemoryMappedFile
. And here is simplest example - Place it into console app. Start 2 instances of it and in quick succession, type w+enter
in one and r+enter
in the other. Watch. Note – I de-synchronized read and write timing so that you can see that sometimes data changes and other times - not
class Program
{
private static MemoryMappedFile _file = MemoryMappedFile.CreateOrOpen("XXX_YYY", 1, MemoryMappedFileAccess.ReadWrite);
private static MemoryMappedViewAccessor _view = _file.CreateViewAccessor();
static void Main(string[] args)
{
askforinput:
Console.WriteLine("R for read, W for write");
string input = Console.ReadLine();
if (string.Equals(input, "r", StringComparison.InvariantCultureIgnoreCase))
StartReading();
else if (string.Equals(input, "w", StringComparison.InvariantCultureIgnoreCase))
StartWriting();
else
goto askforinput;
_view.Dispose();
_file.Dispose();
}
private static void StartReading()
{
bool currVal = false;
for (int i = 0; i < 100; i++)
{
currVal = currVal != true;
Console.WriteLine(_view.ReadBoolean(0));
Thread.Sleep(221);
}
}
private static void StartWriting()
{
bool currVal = false;
for (int i = 0; i < 100; i++)
{
currVal = currVal != true;
_view.Write(0, currVal);
Console.WriteLine("Writen: " + currVal.ToString());
Thread.Sleep(500);
}
}
}
Again, you can build very complex and robust communication layer using Memory Mapped Files. This is just simple example.
Upvotes: 1
Reputation: 12857
Boling it down, use WCF with Named Pipes.
Here is a link that has some meat to it regarding exactly this question: What is the best choice for .NET inter-process communication?
Upvotes: 7