title: Multithreading in c# Monitor class
body: <p>Why do i get the output 1,1,2,3,4,5,6,7,8,9,10 when i use value as lokalvar and 
  the correct ouput 1,2,3,4,5,6,7,8,9,10 when "value" is membervar?</p>
  
  <p>i cant figure out why this happens. Thanks for any explanantions ;)</p>
  
  <pre><code>class Beispiel_3
  {
  
  int value = 0;
  
  public void Execute_Threads()
  {
  // why is the out put wrong when i ise the varibale "value" here?
  //===============================================================
      while (true)
      {
          Monitor.Enter(this);
  
          value++;
          if (value &gt; 20)
              break;
          Console.WriteLine("Zahl = {0}  | Thread = {1}", value, Thread.CurrentThread.GetHashCode().ToString());
          Monitor.Exit(this);
  
      }
  
  }
  public void Execute_MainThread()
  {
      Thread thread1, thread2;
      thread1 = new Thread(Execute_Threads);
      thread2 = new Thread(Execute_Threads);
      thread1.Start();
      thread2.Start();
  
      Console.ReadLine();
  }
  static void Main(string[] args)
  {
      Beispiel_3 bsp3 = new Beispiel_3();
      bsp3.Execute_MainThread();
  
  }
  </code></pre>