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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T19:41:22+00:00 2026-05-13T19:41:22+00:00

If you have two threads within an application, and you don’t want them to

  • 0

If you have two threads within an application, and you don’t want them to run a certain piece of code simultaneously, you can just put a lock around the piece of code, like this:

lock (someObject) {
    // ... some code
}

But how do you do the same thing across separate processes? I thought this is what you use a “global mutex” for, so I tried the Mutex class in various ways, but it doesn’t seem to fulfill my requirements, which are:

  • If you’re the only instance, go ahead and run the code.
  • If you’re the second instance, wait for the first one to finish, then run the code.
  • Don’t throw exceptions.

Problems I ran into:

  • Just instantiating a Mutex object in a using(){...} clause doesn’t seem to do anything; the two instances still happily run concurrently
  • Calling .WaitOne() on the Mutex causes the first instance to run and the second to wait, but the second waits indefinitely, even after the first calls .ReleaseMutex() and leaves the using(){} scope.
  • .WaitOne() throws an exception when the first process exits (System.Threading.AbandonedMutexException).

How do I solve this? Solutions that don’t involve Mutex are very welcome, especially since Mutex appears to be Windows-specific.

  • 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-13T19:41:23+00:00Added an answer on May 13, 2026 at 7:41 pm

    I have two applications:

    ConsoleApplication1.cs

    using System;
    using System.Threading;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                Mutex mutex = new Mutex(false, "AwesomeMutex");
    
                Console.WriteLine("ConsoleApplication1 created mutex, waiting . . .");
    
                mutex.WaitOne();
    
                Console.Write("Waiting for input. . .");
                Console.ReadKey(true);
    
                mutex.ReleaseMutex();
                Console.WriteLine("Disposed mutex");
            }
        }
    }
    

    ConsoleApplication2.cs

    using System;
    using System.Threading;
    
    namespace ConsoleApplication2
    {
        class Program
        {
            static void Main(string[] args)
            {
                Mutex mutex = new Mutex(false, "AwesomeMutex");
                Console.WriteLine("ConsoleApplication2 Created mutex");
    
                mutex.WaitOne();
    
                Console.WriteLine("ConsoleApplication2 got signalled");
    
                mutex.ReleaseMutex();
            }
        }
    }
    

    Starting ConsoleApplication1, followed by ConsoleAplication2 works perfectly with no errors. If your code still bombs out, its a bug with your code, not the Mutex class.

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

Sidebar

Related Questions

I have two threads in an Android application, one is the view thread, and
I'd like to have two Threads. Let's call them : Thread A Thread B
I'm working on an image processing application where I have two threads on top
In Ruby, I want to have two threads running at the same time, and
I have two threads in a producer-consumer pattern. Code works, but then the consumer
I have two threads main thread and worker thread. What I want to be
we have two threads accessing one list via a synchronized method. Can we a)
I have two threads. One of them writes to PipedOutputStream, another one reads from
i have two activities within an ActivityGroup, one ProvinceActivity and another CityActivity. so code:
I have two threads, one needs to poll a bunch of separate static resources

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.