void MethodA()
{
Monitor.Enter(this);
if(someCondition)
{
Monitor.Exit(this);
// This point
MethodB();
}
else
{
// Set some values only
Monitor.Exit(this);
}
}
If I have the above method which can be called in multi threads:
- Assume thread 1 is at
//This point - Another thread enters
Monitor.Enterwhile thread 1 is still at//This point - Will this stop
MethodBfrom being executed? If yes, is there a way of gettingMethodBto execute.
I need to release MethodA before executing MethodB() because I can’t wait for MethodB to complete before releasing MethodA. Also, I cannot start MethodB in a new thread.
Since, someCondition is not passed as a parameter, I could only assume the someCondition could be changing at any time (possibly an instance variable to the class). So, I would write the code this way:
If not, than the previous answer with
conditiondeclared locally would suit you.