Esto es una continuación de previous question con respecto al bloqueo en dos objetos List (Of T). La respuesta fue útil pero me dejó otra pregunta.Forma correcta de usar SyncLock (en general)
Supongamos que tengo una función como esta:
Public Function ListWork() As Integer
List1.Clear()
..Some other work which does not modify List1..
List1.AddRange(SomeArray)
..Some more work that does not involve List1..
Retrun List1.Count
End Function
que reside en una clase que declara Lista1. En un entorno multiproceso, ahora entiendo que debería tener un objeto de bloqueo privado para List1 y bloquear List1 cada vez que se modifique o enumere. Mi pregunta es, ¿debería hacer esto:
Private List1Lock As New Object
Public Function ListWork() As Integer
SyncLock List1Lock
List1.Clear()
End SyncLock
..Some other work which does not modify List1..
SyncLock List1Lock
List1.AddRange(SomeArray)
End SyncLock
..Some more work that does not involve List1..
SyncLock List1Lock
Dim list1Count As Integer = List1.Count
End SyncLock
Retrun list1Count
End Function
o esto:
Private List1Lock As New Object
Public Function ListWork() As Integer
SyncLock List1Lock
List1.Clear()
..Some other work which does not modify List1..
List1.AddRange(SomeArray)
..Some more work that does not involve List1..
Dim list1Count As Integer = List1.Count
End SyncLock
Retrun list1Count
End Function
supongo que el ejemplo anterior es óptima?
Creo que entiendo esto ahora. Bastante sutil. Debo tomar una clase o algo ... Gracias por la información. –