title: Multithreading scenario for list
body: <p>I've read this patterns but found it not to work. I get a rare exception that an item in foreach was changed.</p>
  
  <p>lock (mylist)
  {
  foreach(var a in myList) {}</p>
  
  <p>myList = new List&lt;>() (or myList.Clear() )
  }</p>
  
  <p>I also tried this</p>
  
  <p>foreach(var a in myList.ToList() ) { }</p>
  
  <p>And that also generated exceptions. There are some other patterns described in this thread but I want to confirm/understand why above patterns didnt work
  I've read a bit about how to properly lock a list. The exception doesn't occur often-- just very rarely and there was also a memory leak at the time. </p>
  
  <p>1.
  Do I need to use the lock everywhere I modify myList or does the lock prevent anyone from editing mylist? This may be the source of confusion.</p>
  
  <p>2.</p>
  
  <p>Is there a difference in lock mylist and casting and using syncroot?</p>
  
  <p>See here</p>
  
  <p><a href="https://stackoverflow.com/questions/1362995/properly-locking-a-listt-in-multithreaded-scenarios">Properly locking a List&lt;T&gt; in MultiThreaded Scenarios?</a></p>