0

I don't understand this behavior:

static Mutex Mut;

    static void Main(string[] args)
    {
        try
        {
            Mut = System.Threading.Mutex.OpenExisting("testmut");
        }
        catch
        {
            Mut = new Mutex(true, "testmut");
        }

        Mut.WaitOne();

        Thread.Sleep(4000);

        Mut.ReleaseMutex();
    }

Starting two instances of this application simultaneously will cause the second application to get an Abandoned Mutex Exception after the first process terminates. Why? I did explicitly release the mutex before terminating there in the first process.

user4520
  • 3,401
  • 1
  • 27
  • 50
  • 2
    The WaitOne method blocks the current thread until the Mutex has been released. Therefore, after the WaitOne(), you cannot Release it again. – Roy Dictus Apr 17 '14 at 09:19

1 Answers1

0

Okay, the problem was simple. I actually gained Mutex ownership twice in the first process, first by calling Mut = new Mutex(true, "testmut"); (the true flag grants the caller ownership of the Mutex), and then doing a WaitOne(). Obviously, since I only freed it once in the end, I was getting an AM Exception.

user4520
  • 3,401
  • 1
  • 27
  • 50