Created
September 13, 2017 05:10
-
-
Save carstengehling/9ea4edfe9d9656c63a66a76630ec514f to your computer and use it in GitHub Desktop.
Let only on process do work
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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