在ASP.NET C#code互斥的释放问题 [英] Mutex release issues in ASP.NET C# code
问题描述
我不完全知道如何解决这个问题。我有一个声明为这样一个互斥体:
I'm not exactly sure how to address this issue. I have a mutex that is declared as such:
public class MyNamedLock
{
private Mutex mtx;
private string _strLkName;
public MyNamedLock(string strLockName)
{
_strLkName = strLockName;
//...
mtx = new Mutex(false, _strLkName, out bCreatedNew, mSec);
}
public bool enterLockWithTimeout(int nmsWait = 30 * 1000)
{
_nmsWaitLock = nmsWait;
//Wait
return mtx.WaitOne(nmsWait);
}
public void leaveLock()
{
_nmsWaitLock = 0;
//Release it
mtx.ReleaseMutex();
}
}
然后,它在ASP.NET页面被用作这样的:
Then it is used in an ASP.NET page as such:
public class MyClass
{
private MyNamedLock gl;
public MyClass()
{
gl = new MyNamedLock("lock name");
}
public void funct()
{
try
{
//Enter lock
if (gl.enterLockWithTimeout())
{
//Do work
}
else
throw new Exception("Failed to enter lock");
}
finally
{
//Leave lock
gl.leaveLock();
}
}
}
这code没有给我任何麻烦,在我的开发环境,但生产它有时会引发此异常:
This code doesn't give me any trouble in my dev environment but in the production it sometimes throws this exception:
对象同步方法是从一个同步的块被称为
的code。
Object synchronization method was called from an unsynchronized block of code.
描述是有点模糊,但只是在做我发现异常是在 mtx.ReleaseMutex()提出的痕迹;
部分。是什么意思,如何解决?
The description is kinda vague, but just doing the trace I found out that the exception is raised at the mtx.ReleaseMutex();
part. What does it mean and how to fix it?
推荐答案
您对您的类中的一些问题,并在你的方式使用它。
You have some issues on your class, and on the way you use it.
- 您必须释放只有当你有previous锁定互斥(这是你的错误)
- 您需要关闭并处置您打开互斥
- 同样是更好的只是你要使用它,而不是当你创建你上课之前创建它
MyClass的
。
- You must release the mutex only if you have previous locked (and this is your error)
- You need to Close and Dispose your opened mutex
- Also is better to create it just before you going to use it and not when you create you class
MyClass
.
所以我建议在第一次看改变你的类:
So I suggest at first look to change your class as:
public class MyNamedLock
{
private Mutex mtx = null;
private string _strLkName;
// to know if finally we get lock
bool cNeedToBeRelease = false;
public MyNamedLock(string strLockName)
{
_strLkName = strLockName;
//...
mtx = new Mutex(false, _strLkName, out bCreatedNew, mSec);
}
public bool enterLockWithTimeout(int nmsWait = 30 * 1000)
{
_nmsWaitLock = nmsWait;
bool cLock = false;
try
{
cLock = mtx.WaitOne(nmsWait, false);
cNeedToBeRelease = cLock;
}
catch (AbandonedMutexException)
{
// http://stackoverflow.com/questions/654166/wanted-cross-process-synch-that-doesnt-suffer-from-abandonedmutexexception
// http://msdn.microsoft.com/en-us/library/system.threading.abandonedmutexexception.aspx
cNeedToBeRelease = true;
}
catch (Exception x)
{
// log the error
Debug.Fail("Check the reason of fail:" + x.ToString());
}
return cLock;
}
public void leaveLock()
{
_nmsWaitLock = 0;
if (mtx != null)
{
if (cNeedToBeRelease)
{
try
{
mtx.ReleaseMutex();
cNeedToBeRelease = false;
}
catch (Exception x)
{
Debug.Fail("Check the reason of fail:" + x.ToString());
}
}
mtx.Close();
mtx.Dispose();
mtx = null;
}
}
}
这必须调用类的方式:
public class MyClass
{
public MyClass()
{
}
public void funct()
{
var gl = new MyNamedLock("lock name");
try
{
//Enter lock
if (gl.enterLockWithTimeout())
{
//Do work
}
else
throw new Exception("Failed to enter lock");
}
finally
{
//Leave lock
gl.leaveLock();
}
}
}
这篇关于在ASP.NET C#code互斥的释放问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!