title: Does synclock not work on collections?
body: <p>I have a multithreaded program where many threads may write to a collection and one thread processes the collection at fixed intervals. </p>
  
  <p>Every few hours the application crashes with an error saying "Collection was modified; enumeration operation may not execute" with the debugger pointing to to the method that clears the collection.</p>
  
  <p>Here is the entire code that addresses this collection:</p>
  
  <pre><code>Private ui_add_queue As New List(Of Object)
  
  Private Sub ui_add(recipient_id As String, new_id As String, Optional title As String = "", Optional hue As String = "0.6")
      SyncLock ui_add_queue
          ui_add_queue.Add({recipient_id, new_id, title, hue})
      End SyncLock
  End Sub
  
  Private Sub ui_interval_update()
      SyncLock ui_add_queue
          For Each item In ui_add_queue
              root.browser.Document.InvokeScript("logblock_add", item) &lt;- crashes here
          Next
          ui_add_queue.Clear()
      End SyncLock
  End Sub
  </code></pre>
  
  <p>Both instances of accessing the collection are surrounded by a synclock, shouldn't this prevent this sort of thing from happening? How is it possible for the collection to be modified?</p>
  
  <p>Also, I've red some other answers suggesting to clone the collection and then process the clone but this isn't really a 100% fix. Even if the odds are small, some data might go missing like that if the collection is being modified by one thread while the other is also working on it.</p>