void MethodA()
{
Monitor.Enter(this);
if(someCondition)
{
Monitor.Exit(this);
// This point
MethodB();
}
else
{
// Set some values only
Monitor.Exit(this);
}
}
如果我有上述方法,其可以在多線程調用:適當使用Monitor.Exit的
- 假定線程1處於
//This point
- 另一個線程進入
Monitor.Enter
而線程1仍在//This point
- 這會停止
MethodB
被執行嗎?如果是的話,有沒有辦法讓MethodB
執行。
我需要執行MethodB()
因爲我不能等待MethodB
釋放MethodA
之前完成之前釋放MethodA
。另外,我無法在新線程中啓動MethodB
。
如果發生異常鎖永遠不會被釋放,只能在'try .. finally'中使用它 – Andrey 2010-11-03 16:37:06