1

How to notify listeners that MemoryMappedFile created via CreateNew() has been changed? I do not have MemoryMappedFile physically on disc. I want second process starts reading from MMF when MMF is changed by first process. Similar to FileSystemWatcher.Changed event.

MemoryMappedFile mmf = MemoryMappedFile.CreateNew("testmap", 10000);
// Second process has been started and waiting for data in MMF                                          
// Writing could be performed multiple times
using (MemoryMappedViewStream stream = mmf.CreateViewStream())
{
     BinaryWriter writer = new BinaryWriter(stream);
     writer.Write("hello world!");
}

Second process code:

// The code below should executes when writing to "testmap" file was finished. 
using (MemoryMappedFile mmf = MemoryMappedFile.OpenExisting("testmap"))
{
     using (MemoryMappedViewStream stream = mmf.CreateViewStream())
     {
          BinaryReader reader = new BinaryReader(stream);
          Console.WriteLine("Process A says: {0}", reader.ReadString());
     }               
}

Updated. Solution is to use Named Pipes MSDN as it was suggested by Piotr.

Jasta
  • 31
  • 1
  • 3

1 Answers1

1

I would suggest using Observer design pattern. Something like this:

    void Main()
    {
        Observable observable = new Observable();
        Observer observer = new Observer();
        observable.SomethingHappened += observer.HandleEvent;

        observable.SaveMemoryMappedFile();
    }

    class Observable
    {
        public event EventHandler SomethingHappened;

        public void SaveMemoryMappedFile()
        {
            MemoryMappedFile mmf = MemoryMappedFile.CreateNew("testmap", 10000);
            // Second process has been started and waiting for data in MMF                                          
            // Writing could be performed multiple times
            using (MemoryMappedViewStream stream = mmf.CreateViewStream())
            {
                BinaryWriter writer = new BinaryWriter(stream);
                writer.Write("hello world!");
            }
        }
    }

    class Observer
    {
        public void HandleEvent(object sender, EventArgs args)
        {
            // The code below should executes when writing to "testmap" file was finished. 
            using (MemoryMappedFile mmf = MemoryMappedFile.OpenExisting("testmap"))
            {
                using (MemoryMappedViewStream stream = mmf.CreateViewStream())
                {
                    BinaryReader reader = new BinaryReader(stream);
                    Console.WriteLine("Process A says: {0}", reader.ReadString());
                }
            }
        }
    }

Of course, it must be changed to fit your need.

Piotr
  • 1,155
  • 12
  • 29
  • Thanks, Piotr. But the problem is that "Observable" - its one windows process and "Observer" its second windows process. I am trying to implement some inter process communication with short data transfer. I thought to use MMF for transfering data, but I've faced with impossibility to get "DataReceived" event after finishing writing to MMF – Jasta Aug 02 '17 at 13:28
  • Are those 2 processes on the same machine? – Piotr Aug 02 '17 at 13:45
  • Yes, on the same – Jasta Aug 02 '17 at 13:52
  • For the moment - the way I see it add some interprocess communication by those two processes (wcf, com). You can check for example this: https://stackoverflow.com/questions/528652/what-is-the-simplest-method-of-inter-process-communication-between-2-c-sharp-pro – Piotr Aug 02 '17 at 13:56
  • You can use a [Cross Process EventWaitHandle](http://www.albahari.com/threading/part2.aspx#_CrossProcess_EventWaitHandle) to get notification across different processes. – Bradley Uffner Dec 11 '17 at 13:06