Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi All,

I am using Mutex in a multithreaded application where each threads trying to acquire mutex with same name.

My code looks like

Mutex mutex = null;
string mutexName = "getMutex";
try {
mutex = Mutex.OpenExisting(mutexName);
}

catch (Exception e) {
mutex = new Mutex(false, mutexName);
}


for first thread it runs good but when second thread come and try to execute it it throws an exception and returns.

Exception:
Access to the path 'getMutex' is denied.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)

when i checked on MSDN it show that it may throw UnauthorizedAccessException but don't know how to resolve it and work and why it throw this exception when Mutex object is thread of same user.


Thanks,
Posted

1 solution

You're using the wrong overload of OpenMutex. The first thread to grab the mutex owns it. If any other thread wants to open the mutex, they need to specify the appropriate access rights.

You should be using Mutex.OpenExisting(string, MutexRights)[^]
 
Share this answer
 
Comments
golf89 4-Jul-13 0:12am    
Hi Dave,


When first thread come to execute this code will throw an exception (WaitHandleCannotBeOpenedException)as there is no mutex object with that name and in catch new object is created with same name and non ownership.

And Mutex.OpenExisting(string, MutexRights) also throw same exception

Thanks,
Dave Kreskowiak 4-Jul-13 11:30am    
Without seeing your actual code, it's pretty much impossible to tell you what you're doing wrong.
ewolfman 21-Jan-16 1:12am    
Hi golf89,

Did you ever get to the bottom of this problem?

UPDATE: This solved my problem: http://stackoverflow.com/a/19717341/3208034
Dave Kreskowiak 21-Jan-16 10:49am    
Open your own question and describe your problem completely. It's very unlikely you're ever going to get a reply to this, seeing as you replied to me instead of the OP.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900