In a c# threading app, if I were to lock an object, let us say a queue, and if an exception occurs, will the object stay locked? Here is the pseudo-code:
int ii; lock(MyQueue) { MyClass LclClass = (MyClass)MyQueue.Dequeue(); try { ii = int.parse(LclClass.SomeString); } catch { MessageBox.Show('Error parsing string'); } }
As I understand it, code after the catch doesn’t execute – but I have been wondering if the lock will be freed.
First; have you considered TryParse?
The lock will be released for 2 reasons; first,
lockis essentially:Second; you catch and don’t re-throw the inner exception, so the
locknever actually sees an exception. Of course, you are holding the lock for the duration of a MessageBox, which might be a problem.So it will be released in all but the most fatal catastrophic unrecoverable exceptions.