I need to read from a list on a (background) thread while it may be updated from another (main thread). So I try to just make a temporary list not to access the original object. As updates may occur on multiple places, it would be convenient to place SyncLock on the read logic. Is that inherently wrong? What are my options for locking this correctly, or other ways to acquire an accessible copy of the list in a multithread condition?
' In Main thread:
Public SomeList = New List(Of SomeClass)
' ..edit list
' In other thread:
Dim tempList As List(Of SomeClass)
SyncLock SomeList
tempList = SomeList.ToList
End SyncLock
SomeList.ToList throws:
ArgumentException, Destination array was not long enough. Check destIndex and length, and the array's lower bounds.