"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
 
Back
Top