Rohit
Rohit

Reputation: 3638

Mutex not found even when OS reports that it exists

I am creating and using mutex in a windows service

using(var m = new Mutex(false,"mymutex")
{
    m.WaitOne();
    //to my things for a long time
    m.ReleaseMutex();
 }

On another program running with Administrator rights I do

Mutex.OpenExisting("mymutex")

and it throws mutex does not exist. I can see in the Resource manager that windows service has reference to the mutex.

What is wrong?

Upvotes: 2

Views: 481

Answers (2)

Hans Passant
Hans Passant

Reputation: 942000

Operating system objects like Mutex have session scope. Your service runs in session 0 so its mutex is not visible to processes that run on the desktop session. The workaround is simple, prefix Global\ to the mutex name.

Upvotes: 2

Rohit
Rohit

Reputation: 3638

Ignorance is not bliss. EventWaitHandle.OpenExisting throws WaitHandleCannotBeOpenedException

It looks like the default behaviour changes from a Localsystem account to user account.

Upvotes: 0

Related Questions