The Mutex class in .NET is a little bit tricky to use.
Here’s an example how I got it to do what I want:
/// A simple, cross application mutex. Use <see cref="Acquire"/> to acquire it
/// and release it via <see cref="Dispose"/> when you're finished.
/// Only one thread (and thus process) can have the mutex acquired at the same
public class SimpleMutex : IDisposable
private readonly Mutex m_mutex;
/// Acquires the mutex with the specified name.
/// <param name="mutexName">the mutex's name</param>
/// <param name="timeout">how long to try to acquire the mutex</param>
/// <returns>Returns the mutex or <c>null</c>, if the mutex couldn't be
/// acquire in time (i.e. the current mutex holder didn't release it in
public static SimpleMutex Acquire(string mutexName, TimeSpan timeout)
var mutex = new SimpleMutex(mutexName);
// We could not acquire the mutex in time.
catch (AbandonedMutexException ex)
// We now own this mutex. The previous owner didn't
// release it properly, though.
private SimpleMutex(string mutexName)
this.m_mutex = new Mutex(false, mutexName);
public void Dispose()
You can use it like this:
using (SimpleMutex.Acquire("MyTestMutex", Timeout.InfiniteTimeSpan))
If you run your program twice, one will acquire the mutex and the other one will wait – until you press a key in the first one.
Note: If you forget to call
Dispose() on this mutex, the operating system will make sure that the mutex is released when the program terminates. However, the next process trying to acquire this mutex will then get an
AbandonedMutexException (which is handled properly in