Skip to content

Instantly share code, notes, and snippets.

@ramonsmits
Created May 5, 2017 13:40
Show Gist options
  • Select an option

  • Save ramonsmits/d4e6fc6594e845d38dee3343f1665f71 to your computer and use it in GitHub Desktop.

Select an option

Save ramonsmits/d4e6fc6594e845d38dee3343f1665f71 to your computer and use it in GitHub Desktop.
Global mutex preventing multiple instances
class Program
{
static Mutex SingleInstanceMutex = AcquireGlobalMutex("Receiver");
static Mutex AcquireGlobalMutex(string id, int durationInSeconds = 1)
{
var m = new Mutex(true, @"Global\" + id);
var maxWaitDuration = TimeSpan.FromSeconds(durationInSeconds);
if (!m.WaitOne(maxWaitDuration)) throw new InvalidOperationException("Failed to acquire mutex.");
return m;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment