Solved

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

Posted on 2010-09-14
3
1,002 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 17

Accepted Solution

by:
Daniel Reynolds earned 250 total points
ID: 33676502
swag.... lock is released as it goes out of scope.
0
 
LVL 6

Assisted Solution

by:Aksh
Aksh earned 250 total points
ID: 33676713
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
ID: 33676844
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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

691 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