title: When to use the lock thread in C#?
body: <p>I have a server which handles multiple incoming socket connections and creates 2 different threads which store the data in XML format.</p>
  
  <p>I was using the <code>lock</code> statement for thread safety almost in every event handler called asyncronously and in the 2 threads in different parts of code.  Sadly using this approach my application significantly slows down.</p>
  
  <p>I tried to not use <code>lock</code> at all and the server is very fast in execution, even the file storage seems to boost; but the program crashes for reasons I don't understand after 30sec - 1min. of work.</p>
  
  <p>So. I thought that the best way is to use less locks or to use it only there where's strictly necessary.  As such, I have 2 questions:</p>
  
  <ol>
  <li><p>Is the lock needed when I write to the public accessed variables (C# lists) only or even when I read from them ?</p></li>
  <li><p>Is the lock needed only in the asyncronous threads created by the socket handler or in other places too ?</p></li>
  </ol>
  
  <p>Someone could give me some practical guidelines, about how to operate. I'll not post the whole code this time. It hasn't sense to post about 2500 lines of code.</p>