Using shared memory to exchange data between processes
So far, we have been sending messages to other processes on the same computer. With named pipes and sockets, we could have used other machines as well. That’s the beauty of those protocols: they are network agnostic. However, if you are sure you want to stay on the same machine, using pipes or sockets can be a burden. These methods are not the fastest way to communicate. In those cases, you might be better off using shared memory.
Shared memory is effortless to set up. And yes, of course, that comes with downsides. There is almost no way to secure the data or to prevent collisions. However, it is fast; really, really fast. So, let’s look at a sample.
First, we look at how to write data to shared memory:
using System.IO.MemoryMappedFiles; "Ready to write data to share memory.\nPress Enter to do so.".Dump(ConsoleColor.Cyan); Console.ReadLine(); using var mmf = MemoryMappedFile...