Alex Rivera | Logout

try and lock question

Asked 2011-04-06T15:35:41.423
12

i have a question .. is it ok if i have something like this :

try 
{ 
    lock(programLock) 
    {
         //some stuff 1
    }
}
catch(Exception ex) { //stuff 2 }

i am curious if "some stuff 1" causes an exception , does programLock still remains locked ?

Edit
Report

1 Answer

15

No, the lock will be released, lock is roughly equivalent to this:

try
{
    Monitor.Enter(programLock);
    // some stuff 1
}
finally
{
    Monitor.Exit(programLock);
}

(Meaning if an exception is thrown, Monitor.Exit will get called automatically as you exit the scope of the lock statement)

answered 2011-04-06T15:37:50.743

Your Answer