在NET4中,
Monitor.Enter(Object)
标记为已过时:
[ObsoleteAttribute("This method does not allow its caller to reliably release the lock. Please use an overload with a lockTaken argument instead.")]
public static void Enter(
Object obj
)
还有一种新方法
Monitor.Enter(lockObject, acquiredLock)
使用此用法:
bool acquiredLock = false;
try
{
Monitor.Enter(lockObject, ref acquiredLock);
// Code that accesses resources that are protected by the lock.
}
finally
{
if (acquiredLock)
{
Monitor.Exit(lockObject);
}
}
我以前是这样做的:
Monitor.Enter(lockObject);
try
{
// Code that accesses resources that are protected by the lock.
}
finally
{
Monitor.Exit(lockObject);
}
这是错的吗?为什么?
可能是在进入后但在尝试前的一段插曲?
正如eamon-nerbonne所问的:如果在monitor.exit之前的finally中有一个异步异常,会发生什么?
答:
ThreadAbortException
当引发此异常时,
运行时最终执行所有
在结束线程之前阻止。