Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

try and lock question

Tags:

c#

.net

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 ?

like image 567
Alex Avatar asked Apr 06 '11 15:04

Alex


People also ask

What is a lock question?

Locking a question or an answer means the question, or the answer can no longer receive comments.

What is the answer to the lock puzzle?

It has to be 4. That means the final lock combination is 042—that's right, “the answer to the ultimate question of life, the universe, and everything,” as Douglas Adams imagined it.

What is 3 digit numeric lock code?

3 digit numeric lock code is a form of puzzle game that is being sent on WhatsApp groups among friends and families. Read on to know the answer to the puzzle. The Coronavirus pandemic has made people stay indoors as the nation is under lockdown.


1 Answers

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)

like image 102
Kieren Johnstone Avatar answered Nov 10 '22 23:11

Kieren Johnstone