How to write correctly when an operation requires both at the same time?
try { lock(locker) { .... } } catch(...) { ... } or so
lock(locker) { try { ... } catch(...) { ... } } And how to be in case of an exception, if it is locked?
How to write correctly when an operation requires both at the same time?
try { lock(locker) { .... } } catch(...) { ... } or so
lock(locker) { try { ... } catch(...) { ... } } And how to be in case of an exception, if it is locked?
On SO, it is strongly not recommended to use try-catch with lock . And advised to do so:
lock(whatever) { try { MakeAMess(); } finally { CleanItUp(); // Либо доводим операцию до конца, либо откатываем в прежнее состояние } } They write that throwing an exception into lock breaks the essence for which this lock exists. Therefore, if an exception occurred, there are two options:
Environment.FailFast()lock .The original can be found here .
Source: https://ru.stackoverflow.com/questions/602719/
All Articles
lockis the abbreviated entry ofMonitor.Enter/Exitwithtry-finaly. msdn.microsoft.com/en-us/library/c5kehkcz.aspx - Igor