Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 59255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T17:58:35+00:00 2026-05-10T17:58:35+00:00

The Mutex class is very misunderstood, and Global mutexes even more so. What is

  • 0

The Mutex class is very misunderstood, and Global mutexes even more so.

What is good, safe pattern to use when creating Global mutexes?

One that will work

  • Regardless of the locale my machine is in
  • Is guaranteed to release the mutex properly
  • Optionally does not hang forever if the mutex is not acquired
  • Deals with cases where other processes abandon the mutex
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T17:58:36+00:00Added an answer on May 10, 2026 at 5:58 pm

    I want to make sure this is out there, because it’s so hard to get right:

    using System.Runtime.InteropServices;   //GuidAttribute using System.Reflection;                //Assembly using System.Threading;                 //Mutex using System.Security.AccessControl;    //MutexAccessRule using System.Security.Principal;        //SecurityIdentifier  static void Main(string[] args) {     // get application GUID as defined in AssemblyInfo.cs     string appGuid =         ((GuidAttribute)Assembly.GetExecutingAssembly().             GetCustomAttributes(typeof(GuidAttribute), false).                 GetValue(0)).Value.ToString();      // unique id for global mutex - Global prefix means it is global to the machine     string mutexId = string.Format( 'Global\\{{{0}}}', appGuid );      // Need a place to store a return value in Mutex() constructor call     bool createdNew;      // edited by Jeremy Wiebe to add example of setting up security for multi-user usage     // edited by 'Marc' to work also on localized systems (don't use just 'Everyone')      var allowEveryoneRule =         new MutexAccessRule( new SecurityIdentifier( WellKnownSidType.WorldSid                                                    , null)                            , MutexRights.FullControl                            , AccessControlType.Allow                            );     var securitySettings = new MutexSecurity();     securitySettings.AddAccessRule(allowEveryoneRule);     // edited by MasonGZhwiti to prevent race condition on security settings via VanNguyen     using (var mutex = new Mutex(false, mutexId, out createdNew, securitySettings))     {         // edited by acidzombie24         var hasHandle = false;         try         {             try             {                 // note, you may want to time out here instead of waiting forever                 // edited by acidzombie24                 // mutex.WaitOne(Timeout.Infinite, false);                 hasHandle = mutex.WaitOne(5000, false);                 if (hasHandle == false)                     throw new TimeoutException('Timeout waiting for exclusive access');             }             catch (AbandonedMutexException)             {                 // Log the fact that the mutex was abandoned in another process,                 // it will still get acquired                 hasHandle = true;             }              // Perform your work here.         }         finally         {             // edited by acidzombie24, added if statement             if(hasHandle)                 mutex.ReleaseMutex();         }     } } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

// A Mutex allows threads mutually exclusive access to a resource. //----------------------------------------------------------------------- class Mutex
I am using a global named mutex for file access synchronization between an ASP.NET
We have an issue with the way we are creating a Mutex . The
In VxWorks, I am creating a mutex with the SEM_INVERSION_SAFE option, to protect against
Is there a way in Java to get a method to lock (mutex) the
Related to this question , what is the best practice for naming a mutex?
Trying to write a PowerShell cmdlet that will mute the sound at start, unless

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.