Messaging through memory-mapped files in .NET C#

We saw in this and this posts how to use memory-mapped files to map an existing file to a memory location that multiple processes had access to on the same machine.

The same key objects, i.e. MemoryMappedFile and MemoryMappedViewAccessor can be used for interprocess messaging purposes. The following code shows how a “server” can create a new shared file mapped to memory. Here we use the CreateNew method for this purpose and give the file a mapping name. Note that this is only an in-memory file, it won’t be saved on disk:

static void Main(string[] args)
	using (MemoryMappedFile memoryMappedFile = MemoryMappedFile.CreateNew("news-channel", 10000))
		using (MemoryMappedViewAccessor viewAccessor = memoryMappedFile.CreateViewAccessor())
			byte[] textBytes = Encoding.UTF8.GetBytes("The world is going down.");
			viewAccessor.WriteArray(0, textBytes, 0, textBytes.Length);					


	Console.WriteLine("Main done...");

Consult the links provided above for a full explanation of the code, I won’t repeat it here. You can put the above code into a C# console application and call it Server or something like that. Note that I block code execution for 100 seconds. Without the call to Thread.Sleep the server will simply write to the memory mapped file and then dispose of the MemoryMappedFile object after the “using” block. In which case the client won’t have a chance to connect to the same memory location:

static void Main(string[] args)
	using (MemoryMappedFile memoryMappedFile = MemoryMappedFile.OpenExisting("news-channel"))
		using (MemoryMappedViewAccessor viewAccessor = memoryMappedFile.CreateViewAccessor())
			byte[] bytes = new byte[100];					
			int res = viewAccessor.ReadArray(0, bytes, 0, bytes.Length);
			string text = Encoding.UTF8.GetString(bytes).Trim('\0');

	Console.WriteLine("Main done...");

Above we call the OpenExisting method to locate an existing memory-mapped file by its name. If it doesn’t exist the method throws an exception. You can put the above code into another C# console application and call it client. Run the server first and then the client before Thread.Sleep finishes in the server. The client should be able to read the message:

Memory mapped file interprocess communication

View the list of posts on Messaging here.

About Andras Nemes
I'm a .NET/Java developer living and working in Stockholm, Sweden.

One Response to Messaging through memory-mapped files in .NET C#

  1. Alex says:

    Thank you Andras! I spent almost 2 hours debugging why I’m getting FileNotFoundException. And your Thread.Sleep() explanation clarified everything. You’ve made my day, sir!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s


A great site

Elliot Balynn's Blog

A directory of wonderful thoughts

HarsH ReaLiTy

A Good Blog is Hard to Find

Softwarearchitektur in der Praxis

Wissenswertes zu Webentwicklung, Domain-Driven Design und Microservices

Technology Talks

on Microsoft technologies, Web, Android and others

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog


Once Upon a Camayoc

Bite-size insight on Cyber Security for the not too technical.

Guru N Guns's

OneSolution To dOTnET.

Johnny Zraiby

Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

%d bloggers like this: