By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,640 Members | 1,581 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,640 IT Pros & Developers. It's quick & easy.

"lock" keyword and exceptions

P: n/a
Hi. Quick question:

- I have a class with fields. There is one method that modifies them.

in this method I have something like

lock (this.lockObject)
{
/// throwing exception here...
}

My quetion is, if an exception is thrown within the lock block, will the
lock be released or will the object remain locked.

Ron
Jun 23 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Ron M. Newman wrote:
Hi. Quick question:

- I have a class with fields. There is one method that modifies them.

in this method I have something like

lock (this.lockObject)
{
/// throwing exception here...
}

My quetion is, if an exception is thrown within the lock block, will the
lock be released or will the object remain locked.

Ron


Hi Ron,

The lock will be released. The lock keyword is a helper clause, that in
essence does this:

///
Monitor.Enter(<lock-object>);
try
{
...
}
finally
{
Monitor.Exit();
}
///

-- Tom Spink
Jun 23 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.