Sebastian
Sebastian

Reputation: 440

Named mutex throws TypeInitializationException when runned as different user at the same time

I have to make sure that there is only one instance of my program running at the same time. After some searching i found this SO question and followed the instructions in this answer. I created a small test application:

using System;
using System.Threading;

namespace Test
{
    class Program
    {
        static Mutex mutex = new Mutex(true, "{8F6F0AC4-B9A1-45fd-A8CF-72F04E6BDE8F}");

        static void Main(string[] args)
        {
            if (mutex.WaitOne(TimeSpan.Zero, true))
            {
                Console.ReadLine();
                mutex.ReleaseMutex();
            }
            else
            {
                Console.WriteLine("Only one instance allowed");
                Console.ReadLine();
            }
        }
    }
}

When i execute the binary for the first time everything is okay and it waits for a return to exit. When i start the application the second time it says "Only one instance allowed". So far everything works as expected. BUT when i start the application under a different user (RunAs or by switching users) it throws a TypeInitializationException. How come? Does anyone has a solution for this?

InnerException: System.UnauthorizedAccessException -> Access to the path "{8F6F0AC4-B9A1-45fd-A8CF-72F04E6BDE8F}" is denied.

Upvotes: 0

Views: 430

Answers (1)

Hans Passant
Hans Passant

Reputation: 942010

Look at the InnerException, that tells you what went wrong. I'd guess at a security exception.

Upvotes: 1

Related Questions