I've made a simple test with a MemoryMappedFile
as msdn says :
2 processes, 1 memory mapped file :
- the first process adds the string "1"
- the first process waits
- the second process adds the string "2" and terminates
- the first process now reads the whole memory mapped file
process A:
using (MemoryMappedFile mmf = MemoryMappedFile.CreateNew("testmap", 10000))
{
bool mutexCreated;
Mutex mutex = new Mutex(true, "testmapmutex", out mutexCreated);
using (MemoryMappedViewStream stream = mmf.CreateViewStream())
{
BinaryWriter writer = new BinaryWriter(stream, Encoding.UTF8);
writer.Write("1");
}
mutex.ReleaseMutex();
Console.WriteLine("Start Process B and press ENTER to continue.");
Console.ReadLine();
mutex.WaitOne();
using (MemoryMappedViewStream stream = mmf.CreateViewStream())
{
BinaryReader reader = new BinaryReader(stream, Encoding.UTF8);
Console.WriteLine("Process A says: {0}", reader.ReadString());
Console.WriteLine("Process B says: {0}", reader.ReadString());
}
mutex.ReleaseMutex();
}
process B:
using (MemoryMappedFile mmf = MemoryMappedFile.OpenExisting("testmap"))
{
Mutex mutex = Mutex.OpenExisting("testmapmutex");
mutex.WaitOne();
using (MemoryMappedViewStream stream = mmf.CreateViewStream(1, 0))
{
BinaryWriter writer = new BinaryWriter(stream, Encoding.UTF8);
writer.Write("2");
}
mutex.ReleaseMutex();
}
The result is :
Hu ?
Where is "1", "2" ?
However, if I run ONLY the first process ( without activating process B) I get :
What am I missing ?
I expect to see :
Process A says: 1
Process B says: 2