Suppose I have the following code:
void DoStuff( SomeClass withObject )
{
Action helper = () =>
{
withObject.RunHelper();
}
lock( withObject ) {
actuallyDoStuff( helper );
}
}
void actuallyDoStuff( Action action )
{
action();
}
you see, the Action helper code is outside the lock. I’d expect that Action is just an unnamed function so calling it would not get control outside the lock and the lock would persists.
Yet I’m not sure.
Will the lock be released or persisted while control is inside Action helper?
Yes, it will.
This will happed because standart control flow will be blocked on the
actuallyDoStuffmethod exetucion, until the end of that exetucion. That delegate that you pass as the parameter is nothing then just pure MSIL code which is injected (or not, I don’t really know, but acts like) into that method and run successefully inside thelockscope.So, the lock will persist because there is no “outside”.