The behavior you are looking for can be achieved with this code (simple version):
using(Mutex mutex = new Mutex(false, "GlobalMutexId"))
{
mutex.WaitOne();
//Do your thing
mutex.ReleaseMutex();
}
Or if you prefer a more reusable approach:
public class MutexFactory
{
private string _name;
public MutexFactory(string name)
{
_name = name;
}
public SingleUseMutex Lock()
{
return new SingleUseMutex(_name);
}
}
public class SingleUseMutex : IDisposable
{
private readonly Mutex _mutex;
internal SingleUseMutex(string name)
{
_mutex = new Mutex(false, name);
_mutex.WaitOne();
}
public void Dispose()
{
_mutex.ReleaseMutex();
_mutex.Dispose();
}
}
Can be used like this:
private void TestFunction()
{
MutexFactory factory = new MutexFactory("YourMutexId");
for (int i = 0; i < 100; i++)
{
// also works for new instances of your program of course
new Thread(Interlocked).Start(factory);
}
}
private void Interlocked(object obj)
{
Guid guid = Guid.NewGuid();
MutexFactory factory = obj as MutexFactory;
using (factory.Lock())
{
Debug.WriteLine(guid.ToString("N") + " start");
//Waste Time
Thread.Sleep(50);
Debug.WriteLine(guid.ToString("N") + " end");
}
}