0

I am implementing an IPC for 2 applications that will run on the same host. Console1 and Console2. Based of MS sample, I was able to come up with these codes:

Console1

private static string TextToSend;
    private static MemoryMappedFile memoryMappedFile;
    private static MemoryMappedViewAccessor accessor;

static void Main(string[] args)
{
    using (MemoryMappedFile mmf = MemoryMappedFile.CreateNew("testmap", 10000))
    {
        bool mutexCreated;
        Mutex mutex = new Mutex(true, "testmapmutex", out mutexCreated);
        using (MemoryMappedViewStream stream = mmf.CreateViewStream())
        {
            BinaryWriter writer = new BinaryWriter(stream);
            writer.Write(1);
        }
        mutex.ReleaseMutex();

        Console.WriteLine("Start Process B and press ENTER to continue.");
        Console.ReadLine();

        Console.WriteLine("Start Process C and press ENTER to continue.");
        Console.ReadLine();

        mutex.WaitOne();
        using (MemoryMappedViewStream stream = mmf.CreateViewStream())
        {
            BinaryReader reader = new BinaryReader(stream);
            Console.WriteLine("Process A says: {0}", reader.ReadBoolean());
            Console.WriteLine("Process B says: {0}", reader.ReadBoolean());
            Console.WriteLine("Process C says: {0}", reader.ReadBoolean());
        }
        mutex.ReleaseMutex();
    }

    OnLoad();

    while (true)
    {
        Console.WriteLine("Enter message: ");
        TextToSend = Console.ReadLine();
        if (!String.IsNullOrEmpty(TextToSend))
        {
            SendMessage();
        }
    }
}

private static void OnLoad()
{
    memoryMappedFile = MemoryMappedFile.CreateNew("testMap", 10000);
    accessor = memoryMappedFile.CreateViewAccessor();
}

private static void SendMessage()
{
    var buffer = ASCIIEncoding.ASCII.GetBytes(TextToSend);
    accessor.Write(54, (ushort)buffer.Length);
    accessor.WriteArray(54 + 2, buffer, 0, buffer.Length);
}

private static void ReadMessage()
{
    var size = accessor.ReadUInt16(54);
    var buffer = new byte[size];
    accessor.ReadArray(54 + 2, buffer, 0, buffer.Length);
    Console.WriteLine(ASCIIEncoding.ASCII.GetString(buffer));
}

Console2

private static string TextToSend;
        private static MemoryMappedFile memoryMappedFile;
        private static MemoryMappedViewAccessor accessor;

        static void Main(string[] args)
        {
            try
            {
                using (MemoryMappedFile mmf = MemoryMappedFile.OpenExisting("testmap"))
                {

                    Mutex mutex = Mutex.OpenExisting("testmapmutex");
                    mutex.WaitOne();

                    using (MemoryMappedViewStream stream = mmf.CreateViewStream(1, 0))
                    {
                        BinaryWriter writer = new BinaryWriter(stream);
                        writer.Write(0);
                    }
                    mutex.ReleaseMutex();
                }
            }
            catch (FileNotFoundException)
            {
                Console.WriteLine("Memory-mapped file does not exist. Run Process A first.");
            }

            Console.ReadLine();
        }

        private static void OnLoad()
        {
            memoryMappedFile = MemoryMappedFile.CreateNew("testMap", 10000);
            accessor = memoryMappedFile.CreateViewAccessor();
        }

        private static void SendMessage()
        {
            var buffer = ASCIIEncoding.ASCII.GetBytes(TextToSend);
            accessor.Write(54, (ushort)buffer.Length);
            accessor.WriteArray(54 + 2, buffer, 0, buffer.Length);
        }

        private static void ReadMessage()
        {
            var size = accessor.ReadUInt16(54);
            var buffer = new byte[size];
            accessor.ReadArray(54 + 2, buffer, 0, buffer.Length);
            Console.WriteLine(ASCIIEncoding.ASCII.GetString(buffer));
        }

My problem is, how do I make each of this 2 applications wait for the message of the other? Meaning, as soon as Console1 sends a message, Console2 gets it and writes it in the console and vice versa. Thank you.

Ibanez1408
  • 4,550
  • 10
  • 59
  • 110
  • Similar to named mutex you can use [named event handle](http://www.albahari.com/threading/part2.aspx#_CrossProcess_EventWaitHandle) to send simple signal. – Sinatr Aug 20 '20 at 06:58
  • I don't know how I can use the Notify/Signal when memory..... – Ibanez1408 Aug 20 '20 at 07:05

0 Answers0