If I do:
ConsoleApp1:
bool mutexCreated;
var mutex = new Mutex(true, "MemoryMappedFileMutex", out mutexCreated);
Console.Write("Run Console App2 then press enter");
Console.Read();
// do work
Thread.Sleep(5000);
mutex.ReleaseMutex(); // makes console app 2 to stop waiting
ConsoleApp2
var mutex = Mutex.OpenExisting("MemoryMappedFileMutex");
mutex.WaitOne();
// continue executing once console app 1 releases mutex
Everything works great. I have to start consoleApp1 first for this algorithm to work though.
Now my qestion is I will like consoleApp2 to act as the server. Therefore I will like to start that application first. The problem is that if I do:
bool mutexCreated;
var mutex = new Mutex(true, "MemoryMappedFileMutex", out mutexCreated);
mutex.WaitOne(); // <------ mutex will not wait why????
if I do mutex.WaitOne() that thread will not wait. In other words I want to start consoleApp2 first and have that application wait until I signal the mutex somehow on console application 1….
In your server app, try calling the constructor with false as the first parameter, so that the calling thread will not own the mutex initially.