Solved

C# - try-catch (with return) in lock

Posted on 2010-09-14
3
977 Views
Last Modified: 2012-05-10
Hello,

I'm looking at the IL of a function similar to this and it looks like when an exception occurs the lock is not released.

Is this correct?

Can anybody elaborate?

P.S. I know there are other ways of doing this but I want to know why the lock is not being released. (or at least doesn't seem to be being released)


int TestFunc()
{
lock(object)
{
try
{
...
return 1;
}
catch(Exception)
{
return -1;
}
}
}

Open in new window

0
Comment
Question by:mac-will
3 Comments
 
LVL 17

Accepted Solution

by:
xDJR1875 earned 250 total points
Comment Utility
swag.... lock is released as it goes out of scope.
0
 
LVL 6

Assisted Solution

by:Aksh
Aksh earned 250 total points
Comment Utility
Forwarding the thought what xDJR1875 has mentioned...

lock(...) totally dependent on the scope. If the program goes out of the scope then "implicitly" it gets released.

Now lets try to find graceful solution to your problem. If the lock(...) encompassed code that is prone to throw an exception then you should use Monitor.Enter/Exit method because "A lock method call internally calls Monitor.Enter(..)"

Your Code with Monitor.Enter/Exit and FINALLY block.

=========================================
int TestFunc()
{
Monitor.Enter(object) //Explicit calling rather than implicit lock-ing
{
try
{
   ...
    return 1;
}
catch(Exception)
{return -1;}

finally { //releasing the lock explicitly
Monitor.Exit(object);
}

}
}

==========================================================

0
 
LVL 8

Author Comment

by:mac-will
Comment Utility
Thanks people.
However I know how it is suppose to work but I wasn't getting the expected behaviour.

I found this and I think this may be what I am seeing.

http://blogs.msdn.com/b/ericlippert/archive/2009/03/06/locks-and-exceptions-do-not-mix.aspx
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now