Clearing the contents of a file with memory mapping in C #

I use a MemoryMappedFile to communicate between two programs. Program "A" creates mmf and reads its contents by timer. Program "B" writes xml data to mmf by timer. I have a memory card, but I ran into a problem when the previous iteration of the XML data is larger than the current and old data is transferred to the next round.

so for simplicity let's say program B writes

AAAA

Program A will read correctly,

Then the following entry from program B:

b

Program a reads

baaa

There seems to be an easy way to dump the contents of a memory mapped file, but I can't figure it out. It is very possible that I am completely wrong in the way I do this.

This is what I am doing right now.

Program A:

using (MemoryMappedFile mmf = MemoryMappedFile.OpenExisting("testmap",MemoryMappedFileRights.ReadWrite)) { Mutex mutex = Mutex.OpenExisting("testmapmutex"); mutex.WaitOne(); string outputtext; using (MemoryMappedViewStream stream = mmf.CreateViewStream(0,0)) { XmlSerializer deserializer = new XmlSerializer(typeof(MyObject)); TextReader textReader = new StreamReader(stream); outputtext = textReader.ReadToEnd(); textReader.Close(); } mutex.ReleaseMutex(); return outputtext; //ends up in a textbox for debugging } 

Program B

 using (MemoryMappedFile mmf = MemoryMappedFile.OpenExisting("testmap", MemoryMappedFileRights.ReadWrite)) { Mutex mutex = Mutex.OpenExisting("testmapmutex"); mutex.WaitOne(); using (MemoryMappedViewStream stream = mmf.CreateViewStream(0, 0)) { XmlSerializer serializer = new XmlSerializer(typeof(MyObject)); TextWriter textWriter = new StreamWriter(stream); serializer.Serialize(textWriter, myObjectToExport); textWriter.Flush(); } mutex.ReleaseMutex(); } 
+7
c #
source share
1 answer

I didn't work very much with MemoryMappedStreams, but this question seemed interesting, so I hacked it. I wrote a really basic Windows example with two buttons (read / write) and one text field. I did not go at β€œ0, 0” to CreateViewStream calls, and I created a fixed-length file using the β€œCreateOrOpen” call, and it worked fine! Below are the key code snippets I wrote:

READ File

 // create the file if it doesn't exist if (sharedFile == null) sharedFile = MemoryMappedFile.CreateOrOpen("testmap", 1000, MemoryMappedFileAccess.ReadWrite); // process safe handling Mutex mutex = new Mutex(false, "testmapmutex"); if (mutex.WaitOne()) { try { using (MemoryMappedViewStream stream = sharedFile.CreateViewStream()) { var writer = new StreamWriter(stream); writer.WriteLine(txtResult.Text); writer.Flush(); } } finally { mutex.ReleaseMutex(); } } 

WRITE File

 // create the file if it doesn't exist if (sharedFile == null) sharedFile = MemoryMappedFile.CreateOrOpen("testmap", 1000, MemoryMappedFileAccess.ReadWrite); // process safe handling Mutex mutex = new Mutex(false, "testmapmutex"); if (mutex.WaitOne()) { try { using (MemoryMappedViewStream stream = sharedFile.CreateViewStream()) { var textReader = new StreamReader(stream); txtResult.Text = textReader.ReadToEnd(); textReader.Close(); } } finally { mutex.ReleaseMutex(); } } 

Dispose of the file (after completion)

 if (sharedFile != null) sharedFile.Dispose(); 

See the full example here: https://github.com/goopyjava/memory-map-test . Hope this helps!

EDIT / NOTE . If you look at the example provided, you can write to the file as many times as you want, and at any time when you read, you will definitely read / only what was written last. I believe this was the original goal of the question.

-one
source share

All Articles