I am wondering about a fail-safe way to call Monitor.TryEnter. The documentation shows it as this:
if (Monitor.TryEnter(lockObj)) {
try {
// The critical section.
}
finally {
// Ensure that the lock is released.
Monitor.Exit(lockObj);
}
}
As this is the 'official' way to call it, i hesitate to do anything else. But i do not feel very comfortable with that code: Suppose we get a ThreadAbortException like this :
if (Monitor.TryEnter(lockObj)) {
// *** ThreadAbortException happens exactly here
try {
[...]
}
finally { [...] }
}
Does that not leave me with a lock that is never released?