
Originally Posted by
pacerier
i think the code u provided above would not work:
Code:
Get
If InSet Then
SyncLock Lock
Return _SomeString
End SyncLock
Else
Return _SomeString
End If
End Get
because after you have Returned, everything else below it does not run. So the Lock never gets released. so what we have is this:
Code:
Get
If InSet Then
SyncLock Lock
End SyncLock
Return _SomeString
Else
Return _SomeString
End If
End Get
No that is not the case, when you come out of the scope of a SyncLock block the lock will be released - it doesnt have to actually hit the End SyncLock line. Just like if you use a Using statement, you dont have to hit the End Using line for the object to be disposed, its just when you come out of the scope of that statement. I assume this behaviour is achieved by the fact that internally the SyncLock uses a Try/Catch/Finally block and releases the lock in the Finally block. You can see the same behaviour yourself if you just do something like this inside a Function:
vb.net Code:
Try
Dim something As String = "Some text"
Return something
Catch
MessageBox.Show("Oh dear")
Finally
MessageBox.Show("Merry Christmas") 'seasonal examples FTW
End Try
Now if you run that function you will see that you get your 'something' object returned but the Finally block is still reached so you should still see the Merry Christmas message box