> Exactly, the "safeness" of it was to try to fix the inherent faults in
> how lock behaves.
Imho, `lock` is fine. In general, you want to enter a state outside of
the try/finally block that exits the state, precisely so that you
don't try to undo what has never been done. Consider, for example,
locking `SomeRef` when SomeRef is null: You do NOT want to try to
acquire the monitor within the try block and release the monitor
within the finally block!
> Monitor.Enter(this);
> // <-- Exception(e.g. threadabort), exit is never called after enter.
[quoted text clipped - 6 lines]
> Monitor.Exit(this);
> }
Yes, this is why thread abort is no safer in managed code that in
unmanaged code.
> Notice that the program proves that lock() is unsafe. I was trying to
> show that the try/enter/finally/leave method was in fact safe. But
> instead I found the error that you also found.
>
> Do you agree that it should not be an error?
No, I think you found a PEBCAC error. The error message you got seems
exactly right.

Signature
<http://www.midnightbeach.com>
Michael Kennedy - 30 Nov 2005 22:45 GMT
I suppose there is no way out of this eh?
The try/enter/finally/leave could prematurely unlock the code if you
have "doublly locked it"
try
{
<-- error here is a double leave
enter
try { enter }
finally {leave}
}
finally {leave}
and
// lock version
enter
<-- error here is a permently locked.
try
{
enter
try { }
finally {leave}
}
finally {leave}
Does ASP.NET only send thread abort exceptions from the main thread?
Thanks,
Michael