Skip to content

Instantly share code, notes, and snippets.

@carstengehling
Created September 13, 2017 05:10
Show Gist options
  • Save carstengehling/9ea4edfe9d9656c63a66a76630ec514f to your computer and use it in GitHub Desktop.
Save carstengehling/9ea4edfe9d9656c63a66a76630ec514f to your computer and use it in GitHub Desktop.
Let only on process do work
public class BackgroundWorker
{
private Mutex mutex = new Mutex(true, "some-unique-id");
public void Run()
{
bool handleAquired = false;
try
{
handleAquired = mutex.WaitOne(1000);
}
catch (AbandonedMutexException)
{
handleAquired = true;
}
if (!handleAquired)
return;
try
{
// Do the stuff
}
finally
{
mutex.ReleaseMutext();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment