I have some code that can be called from inside or outside a lock. I need to do stuff when inside the lock. The code itself has no knowledge of where it's being called from. So, I need something like this:
lock (MyLock) {
if (INSIDE_LOCK) ...
}
I know it sounds weird and wrong but I need this for compatibility issues. Otherwise I will have to rewrite a lot of code, which would be risky since I have no tests.
Try Monitor
class:
if (Monitor.IsEntered(MyLock)) {...}
Since (see René Vogt comment below) lock
lock(MyLock) {
...
}
is, in fact a syntactic sugar for
Monitor.Enter(MyLock);
try {
...
}
finally {
Monitor.Leave(MyLock);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With