Created
May 5, 2017 13:40
-
-
Save ramonsmits/d4e6fc6594e845d38dee3343f1665f71 to your computer and use it in GitHub Desktop.
Global mutex preventing multiple instances
This file contains hidden or 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
| 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