试图获取以前获得的互斥锁的例外情况

我有这些代码行:
// index is an integer
Mutex mutex = new Mutex(false, "MUTEX_PREFIX" + index.ToString());
mutex.WaitOne();
// Access to the shared object which should not be accessed by multiple threads.
mutex.ReleaseMutex();
其他线程使用相同的逻辑(名为mutex),因此我确保正确的锁用于正确的资源。 问题是如果命名的
mutex
被另一个线程(或进程)获取,则当前线程获得异常而不是在第二行上被阻塞。 我需要的是等待命名的
mutex
被释放然后继续我的代码。 我哪里错了?     
已邀请:
完成后必须调用mutex.ReleaseMutex()。你也抓住了所有例外,释放和重新抛出; 典型的模式是使用lock,它可以处理异常:
 lock (typeof(MyClass))
 {
     .. access to the share object
 }
    

要回复问题请先登录注册