Solved

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

Posted on 2010-09-14
3
980 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
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Memory Usage 2 49
asp.net mvc input file 2 32
Unable to upgrade to .Net 4.6.2 on Server 2012r2 2 42
About delegates in c sharp 3 31
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

914 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

19 Experts available now in Live!

Get 1:1 Help Now