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;
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(); }
c #
bfayer
source share