title: Thread safe StreamWriter C# how to do it? 1
body: <p>What is the best way to build a program that is thread safe in terms that it needs to write double values to a file. If the function that saves the values  via streamwriter is being called by multiple threads? Whats the best way of doing it?</p>
  
  <hr>
  
  <p>Code from comment:</p>
  
  <pre><code>    static void Main()
      {
          List&lt;double&gt; Values = new List&lt;double&gt;();
          StreamWriter writer = new StreamWriter("test.out");
  
          for (int i = 0; i &lt; 1000; i++) Values.Add(i);
  
          foreach (double V in Values)
          {
              ThreadPool.QueueUserWorkItem(delegate(object state) { SaveValues(writer, V); }, V);
          }
      }
  
      static void SaveValues(StreamWriter writer, double Value)
      {
          lock (writer) writer.WriteLine(Value);
      } 
  </code></pre>