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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:06:44+00:00 2026-06-11T17:06:44+00:00

I have an application code in which I use a mutex to synchronise some

  • 0

I have an application code in which I use a mutex to synchronise some code during the creation of an object. The object constructor acquires the mutex and ONLY releases it when the object is no longer needed thus one place to release the mutex would be in the object destructor. As I debugged the code using 2 instances of the app, the 1st instance first acquires the mutex, the 2nd instance sits and waits (mut.WaitOne()). User then closes the 1st app instance. At this instance, the 2nd instance mut.WaitOne() throws the exception: “The wait completed due to an abandoned mutex.” This happens before the mut.ReleaseMutex() was called in the 1st instance (I know it because it hit my breakpoint in the object destructor code before calling MutexRelease). It appears that the mutex was released before ReleaseMutex() was called thus causing the exception. How would I resolve this race condition? Thank you for your help.

public sealed class MyObject
{
    static ExtDeviceDriver devDrv;
    private Mutex mut = new Mutex(false,myMutex);

    public MyObject()
    {
        mut.WaitOne();
        //Thread safe code here.
        devDrv = new ExtDeviceDriver();
    }

    ~MyObject()
    {
        mut.ReleaseMutex();
    }
}
  • 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-06-11T17:06:45+00:00Added an answer on June 11, 2026 at 5:06 pm

    Destructors are not deterministic; there is no guarantee by the CLR as to when they will run. Instead, implement IDisposable in your class, and force callers to use its instances in using(...) blocks. That will ensure that your implementation of Dispose gets called when it needs to.

    E.g.,

    public sealed class MyObject : IDisposable
    {
        static ExtDeviceDriver devDrv;
        private Mutex mut = new Mutex(false,myMutex);
    
        public MyObject()
        {
            mut.WaitOne();
            //Thread safe code here.
            devDrv = new ExtDeviceDriver();
        }
    
        public void Dispose() {
            mut.ReleaseMutex();
        }
    
    }
    

    Then callers would just need to do this:

    using (var x = new MyObject()) {
        // etc
    }
    

    When the execution flow exists the using block, Dispose will be called, regardless of exceptions or anything else.

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

Sidebar

Related Questions

I have some VB code which makes use of a COM api and results
I have this code which I use on my swing application: ReportClientDocument rpt =
In my application I have a wrapper over some native code, which is called
I have an application which uses code that produces various types of objects and
In my console application have an abstract Factory class Listener which contains code for
I have an application which was developed under Windows, but for gcc. The code
I have a Java application which interacts with native code using JNI. The native
I have to code in an application which is in Unicode UTF-8 in Windows,
I have some existing custom Excel workbooks/applications with code-behind C#, which work fine. They
I have an application with some bunch of code like this: errCode = callMainSystem();

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.