title: When I use threads to print things to the console, why does it produce odd results?
body: <p>I've been getting into Threads lately while reading the very nice pdf from Rob Miles (<a href="http://www.csharpcourse.com/">here</a>). He had an example on page 160 (2012, C# pdf) but it didn't write to the console just did empty loops.</p>
  
  <p>I wrote a very simple thread producing loop that creates 10 threads that are writing their IDs to the screen on each multiple of 1000.  This was nice - it showed me how threads were running together.  My Questions begins with why is my output so confused?  Often when I run the program below, I get multiple "Thread 3 Finished" lines where I'm pretty certain, I should only have one of each.</p>
  
  <p>I added a <a href="http://msdn.microsoft.com/en-us/library/c5kehkcz%28v=vs.100%29.aspx">"lock"</a> from MSDN to the loop but it still seems to produce odd output (I'll put an example below).</p>
  
  <pre><code>    namespace ConsoleApplication1
      {
          class Program
          {
              static object sync = new object();
  
              static void Main(string[] args)
              {
                  for (int i = 0; i &lt; 10; i++)
                  {
                      Thread myThread = new Thread(() =&gt; DoThis(i));
                      myThread.Start();
                  }
                  Console.ReadLine();
              }
  
              static void DoThis(int s)
              {
                  lock (sync) // a new addition that hasn't helped
                  {
                      for (long j = 0; j &lt; 10000; j++)
                      {
                          if (j % 1000 == 0) Console.Write(String.Format("{0}.", s));
                      }
                      Console.WriteLine("\r\nThread {0} Finished", s);
                      Debug.Print("\r\nThread {0} Finished", s); //&lt;-- added to debug
  
                  }
              }
          }
      }
  </code></pre>
  
  <p>I thought I was doing okay - I have local variables (my counting loop), I have an int that is passed presumably not by reference and later I tried to lock it while doing it's loop.  No joy.What would I need to do to make the output look sensible?  I tried Deubg.Print to troubleshoot but it has errors too (below).</p>
  
  <p>Eventually, I want to use threading in a larger application but if I can't get it right here, I'm not sure I want to!</p>
  
  <p>Example Output from the debug.print line at the end: (note the multiples) ...</p>
  
  <blockquote>
  <pre><code>Thread 1 Done
  The thread '&lt;No Name&gt;' (0x15cc) has exited with code 0 (0x0).
  
  Thread 9 Done
  The thread '&lt;No Name&gt;' (0x1d0c) has exited with code 0 (0x0).
  
  Thread 6 Done
  The thread '&lt;No Name&gt;' (0x2248) has exited with code 0 (0x0).
  
  Thread 10 Done
  The thread '&lt;No Name&gt;' (0x22bc) has exited with code 0 (0x0).
  
  Thread 9 Done
  The thread '&lt;No Name&gt;' (0x85c) has exited with code 0 (0x0).
  
  Thread 9 Done
  The thread '&lt;No Name&gt;' (0x1628) has exited with code 0 (0x0).
  
  Thread 3 Done
  The thread '&lt;No Name&gt;' (0x2384) has exited with code 0 (0x0).
  
  Thread 6 Done
  
  Thread 2 Done
  
  Thread 4 Done
  The thread '&lt;No Name&gt;' (0x2348) has exited with code 0 (0x0).
  The thread '&lt;No Name&gt;' (0x2420) has exited with code 0 (0x0).
  The thread '&lt;No Name&gt;' (0x1ea8) has exited with code 0 (0x0).
  </code></pre>
  </blockquote>
  
  <p>Let me know if I can offer any more info on what I've tried.</p>