Mutex.OpenExisting MSDN引发异常 [英] Mutex.OpenExisting MSDN throws an exception

查看:199
本文介绍了Mutex.OpenExisting MSDN引发异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我在多线程应用程序中使用Mutex,其中每个线程都试图获取具有相同名称的互斥锁。



我的代码看起来像



Mutex mutex = null;

string mutexName =getMutex;

尝试{

mutex = Mutex.OpenExisting(mutexName);

}



catch (例外e){

mutex = new Mutex(false,mutexName);

}





第一个线程它运行良好但是当第二个线程到来并尝试执行它时会抛出异常并返回。



例外:

拒绝访问路径'getMutex'。

在System.IO .__ Error.WinIOError(Int32 errorCode,String maybeFullPath)



,当我在MSDN上检查时,它显示它可能抛出UnauthorizedAccessException但不知道如何解决它并且工作以及为什么当Mutex对象是同一用户的线程时抛出此异常。




谢谢,

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,

推荐答案

你正在使用错误的重载OpenMutex。抓取互斥锁的第一个线程拥有它。如果任何其他线程想要打开互斥锁,他们需要指定适当的访问权限。



您应该使用Mutex.OpenExisting(string,MutexRights) [ ^ ]
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)[^]


这篇关于Mutex.OpenExisting MSDN引发异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆