"Robin Tucker" <rt******@hotmail.comschrieb im Newsbeitrag
news:f3*******************@news.demon.co.uk...
>That could lead to a deadly embrace if another thread does this:
SyncLock objB
SyncLock objA
End SyncLock
End SyncLock
Deadlock indeed.
However, the only other thing I can think of is to encapsulate the objects
that require locking into one object and to lock that.
Yea, I was getting deadlocks.
The problem is that it was not just two items, but there is a list of items
and different threads iterate through the list and try to modify some item
and if needed a related item. That related item might itself be the related
item of another item being modified by another thread, hence the problem.
Well I found that I could at least create an ugly but workable workaround
using Monitor.TryEnter:
Dim worked As Boolean = False
While Not worked
SyncLock objA
worked = Threading.Monitor.TryEnter(objB, 100)
If worked Then
Try
' Do Stuff
Catch ex As Exception
Throw ex
Finally
Threading.Monitor.Exit(objB)
End Try
End If
End SyncLock
End While
But I don't like it one bit since it doesn't automatically release the lock
(hence I need the try catch finally).
Is there a better way?
In any case, thanks for the help!