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 7444595
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T11:32:47+00:00 2026-05-29T11:32:47+00:00

I am using a named system mutex to synchronise 2 processes. This is how

  • 0

I am using a named system mutex to synchronise 2 processes. This is how I am currently acquiring a mutex within my application:

using System.Threading;

public static bool AcquireMutex()
{
    // Protect against double acquisitions
    if (MyMutex != null)
    {
        throw new ApplicationException("Failed to acquire mutex");
    }

    try
    {
        // See if a named system mutex has already been created - if it has,
        // wait a short amount of time for its release.
        MyMutex = Mutex.OpenExisting(MutexName);
        if (!MyMutex.WaitOne(TimeSpan.FromSeconds(2), false))
        {
            // MyMutex still being held
            MyMutex = null;
            return false;
        }
    }
    catch
    {
        // MyMutex doesn't exist so create it
        MyMutex = new Mutex(true, MutexName);
    }

    return true;
}

OpenExisting will throw an exception if the named system mutex with MutexName doesn’t exist, allowing my application to create it.

However, there seems to be a race condition here – if OpenExisting throws, there is a small window before the call to new Mutex where the other application may have acquired the mutex.

What is the best way to avoid this race condition and make this code more reliable?

A colleague mentioned that he was using CreateMutex from the Win32 Platform SDK in his code (the other process which needs to be synchronised). This doesn’t seem to be natively supported by the .NET Framework, however. So I’m not sure it’s the best solution for my code.


Update

Based on the answer from @David Schwartz, here is my new code:

public static bool AcquireMutex()
{
    // Protect against double acquisitions
    if (MyMutex != null)
    {
        throw new ApplicationException("Failed to acquire mutex");
    }

    bool createdNew;
    MyMutex = new Mutex(true, MutexName, out createdNew);
    if (createdNew)
    {
        // Mutex was created so ownership is guaranteed; no need to wait on it.
        return true;
    }

    try
    {
        if (!MyMutex.WaitOne(TimeSpan.FromSeconds(2), false))
        {
            MyMutex = null;
            return false;
        }
    }
    catch (AbandonedMutexException)
    {
        // Other application was aborted, which led to an abandoned mutex.
        // This is fine, as we have still successfully acquired the mutex.
    }

    return true;
}
  • 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. Editorial Team
    Editorial Team
    2026-05-29T11:32:48+00:00Added an answer on May 29, 2026 at 11:32 am

    There’s a constructor specifically designed for this purpose. From the docs:

    createdNew
    Type: System.Boolean
    When this method returns, contains a Boolean that is true if a local mutex was created (that is, if name is null or an empty string) or if the specified named system mutex was created; false if the specified named system mutex already existed. This parameter is passed uninitialized.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using a named mutex to detect other instances of my application and exit
Prior to .net 4.0, I implemented a solution using named data slots in System.Threading.Thread.
using System; using System.Threading; public class Example { // mre is used to block
I am using a global named mutex for file access synchronization between an ASP.NET
Should I implement named parameters in Java using Hash tables? I saw this entry:
i am using a named pipe for IPC on a Debian system. I will
I am coding a forum system using PHP. I am currently storing a threads
I am using a named pipe for communications between two processes and want to
I have the following .gitignore file # file named .gitignore system/application/config/database.php system/application/config/config.php system/logs modules/recaptcha/config/*
I have an web service named AEWService.asmx with the following code: using System; using

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.