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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T23:37:24+00:00 2026-05-28T23:37:24+00:00

I have created the following test program: static void Main(string[] args) { using (var

  • 0

I have created the following test program:

static void Main(string[] args)
{
    using (var mutex = new Mutex(false, "foobar"))
    {
        Console.WriteLine("Created mutex");
        try
        {
            try
            {
                if (!mutex.WaitOne(TimeSpan.FromSeconds(5), false))
                {
                    Console.WriteLine("Unable to acquire mutex");
                    Environment.Exit(0);
                }
            }
            catch (AbandonedMutexException)
            {
                Console.WriteLine("Mutex was abandoned");
            }

            Console.WriteLine("Acquired mutex - sleeping 10 seconds");
            Thread.Sleep(10000);
        }
        finally
        {
            mutex.ReleaseMutex();
            Console.WriteLine("Released mutex");
        }
    }

The idea is that I run the program, and while the thread is sleeping for 10 seconds, I kill the process via task manager. Next time I run the process, I’m expecting that the AbandonedMutexException would be caught on the WaitOne() call. But I’m not seeing the output “Mutex was abandoned”.

The MSDN documentation mentions the following:

When a thread abandons a mutex, the exception is thrown in the next
thread that acquires the mutex.

However, it looks like the OS is releasing the mutex when my process is killed (rather than another thread within the same application).

Is there a way for me to be able to detect a mutex abandoned in this manner?

  • 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-28T23:37:25+00:00Added an answer on May 28, 2026 at 11:37 pm

    What you’re observing is the correct behavior. From the documentation:

    The named mutex is a system object whose lifetime is bounded by the lifetimes of the Mutex objects that represent it. The named mutex is created when the first process creates its Mutex object; in this example, the named mutex is owned by the first process that runs the program. The named mutex is destroyed when all the Mutex objects that represent it have been released.

    If you run your program in two separate processes simultaneously (perhaps increasing the timeouts) and kill the first process, you can observe your expected behavior. When the first process is killed, it abandons the Mutex, which allows the second process to obtain the mutex, at which point it immediately throws an AbandonedMutexException.

    If you want to determine whether your process exited gracefully, you need a different mechanism. You could accomplish this a number of ways, but one of the simplest would be to have the process create a file in a known location at startup time and delete that file after a graceful cleanup. Then you can check for that file, and if you find the file (and no running instance of your program), you know the last shutdown was not graceful or was not completed successfully.

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

Sidebar

Related Questions

I have the following code: class Program { static void Main(string[] args) { using
I have created the fallowing Sample-Code: class Program { static void Main(string[] args) {
Consider the following console application: class Program { static void Main() { MyInterface test
As a debugger/test program for my brainf*ck implementation, I have created the following counting
I have created the following project structure for my new asp.net mvc project any
Hey Guys, i have created the following Menu Structure: <div id=menu> <ul> <li><a href=#>Main
I have created some application, which is reading from System.in using the following method:
I have created a vector of class objects. The following program crashes with Pointer
I have a program, using pyserial, and I want to test it without using
I have the following test-code: CREATE TABLE #Foo (Foo int) INSERT INTO #Foo SELECT

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.