Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interprocess communication on the same box - communication between 2 applications or processes

Tags:

c#

.net

ipc

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

like image 273
user2843693 Avatar asked Feb 15 '23 03:02

user2843693


2 Answers

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?

like image 71
T McKeown Avatar answered Apr 08 '23 08:04

T McKeown


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.

like image 36
T.S. Avatar answered Apr 08 '23 08:04

T.S.