0

Lets consider a following piece of code:

lock(someObject) {
  try {

  }
  catch (...) {

  }
}  

How to ensure that each this lock always will be released ? I mean some unsupported exceptions for example.

1 Answers1

0

It'll be released as soon as the execution passes the outer brace of the lock. The only way it won't get released is if the execution between the lock(x){ } never terminates e.g like...

lock(x)
{
   if (weAreCrazy)
   {
       while(true)
          Console.Writeline("Haven't we already done this?");
   }
}
<-- if (!weAreCrazy)... lock would be released here
Mick
  • 6,527
  • 4
  • 52
  • 67
  • So, what about non-supported exception ? Flow of code reach always `}` ? –  Mar 21 '17 at 06:36
  • It doesn't matter how the code exits the braces.... its similar to a using(y) { } statement, except a using statement calls dispose on y instead of releasing a lock – Mick Mar 21 '17 at 06:37
  • So in my case lock will be released. Thanks –  Mar 21 '17 at 06:44
  • Not just in your case, but every case... except the one mentioned above – Mick Mar 21 '17 at 07:00