"lock" keyword and exceptions

  • Thread starter Thread starter Ron M. Newman
  • Start date Start date
R

Ron M. Newman

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
 
Ron said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top