MikeGB621234

Given that the lock statement as an alternative to monitor. I have seen a number of cases where the lock code is not wrapped in try/catch/finally, where finally releases the lock. Is there some reason that lock could not work more like using where the try/finally block is generated for you

Or at least a version where it works this way...



Re: Visual C# Language lock with exception....

Peter Ritchie

lock(x)... is precisely equivalent to:

Code Snippet

Object o = x;

Monitor.Enter(o);

try

{

...

}

finally

{

Monitor.Exit(o);

}

Is that what you mean




Re: Visual C# Language lock with exception....

MikeGB621234

Yes but what about the case where code generates an exception At least with testing that I did with a generic try / finally block the finally code does not execute if an exception occurs in the try block.

The reason that I was curious was that we were trying to hunt down abandoned mutex exceptions and trying to understand under what conditions that they occur.





Re: Visual C# Language lock with exception....

Peter Ritchie

In what way, and with what exception Code in a finally block will always get executed unless you terminate the application (or use Thread.Abort, it can interrupt lock statements and leave an object locked without calling the finally block).






Re: Visual C# Language lock with exception....

Figo Fei - MSFT

Peter Ritchie wrote:
In what way, and with what exception Code in a finally block will always get executed unless you terminate the application (or use Thread.Abort, it can interrupt lock statements and leave an object locked without calling the finally block).

Thanks for Peter's input.

Actually, in C# 2.0 or above, even if the Thread.Abort is called, the code in finally block can be executed at last. However, 1.1 or before, ThreadAbortException will leave the finally block unexecuted.

Thanks






Re: Visual C# Language lock with exception....

Peter Ritchie

Figo Fei - MSFT wrote:
Peter Ritchie wrote:
In what way, and with what exception Code in a finally block will always get executed unless you terminate the application (or use Thread.Abort, it can interrupt lock statements and leave an object locked without calling the finally block).

Thanks for Peter's input.

Actually, in C# 2.0 or above, even if the Thread.Abort is called, the code in finally block can be executed at last. However, 1.1 or before, ThreadAbortException will leave the finally block unexecuted.

Thanks

There are cases in .NET 2.0 where the finally block won't be executed after the call to Monitor.Enter from a lock statement. See http://www.bluebytesoftware.com/blog/PermaLink,guid,d9ff204a-a8a5-400e-bcbc-dedb90a7d11a.aspx and http://blogs.msdn.com/ericlippert/archive/2007/08/17/subtleties-of-c-il-codegen.aspx.




Re: Visual C# Language lock with exception....

Figo Fei - MSFT

Peter Ritchie wrote:
Figo Fei - MSFT wrote:
Peter Ritchie wrote:
In what way, and with what exception Code in a finally block will always get executed unless you terminate the application (or use Thread.Abort, it can interrupt lock statements and leave an object locked without calling the finally block).

Thanks for Peter's input.

Actually, in C# 2.0 or above, even if the Thread.Abort is called, the code in finally block can be executed at last. However, 1.1 or before, ThreadAbortException will leave the finally block unexecuted.

Thanks

There are cases in .NET 2.0 where the finally block won't be executed after the call to Monitor.Enter from a lock statement. See http://www.bluebytesoftware.com/blog/PermaLink,guid,d9ff204a-a8a5-400e-bcbc-dedb90a7d11a.aspx and http://blogs.msdn.com/ericlippert/archive/2007/08/17/subtleties-of-c-il-codegen.aspx.

These articles really give a detail look into it, I think the answer has been clear enough.

What I meant in my previous post is that if the Thread.Abort generated in the try block the finally block will truely be executed which we have reached a mutual agreement.

Thanks again Peter Smile