title: Handle leak in .Net threads
body: <p>This has actually bitten me a couple times. If you do simple code like this:</p>
  
  <pre><code>    private void button1_Click(object sender, EventArgs e)
      {
  
          for (int i = 0; i &lt; 1000000; i++)
          {
              Thread CE = new Thread(SendCEcho);
              CE.Priority = ThreadPriority.Normal;
              CE.IsBackground = true;
              CE.Start();
              Thread.Sleep(500);
              GC.Collect();
          }
      }
  
      private void SendCEcho()
      {
          int Counter = 0;
  
          for (int i = 0; i &lt; 5; i++)
          {
              Counter++;
              Thread.Sleep(25);
          }
      }
  </code></pre>
  
  <p>Run this code and watch the handles fly! Thread.Sleep is so you can shut it down and it doesn't take over you computer. This should guarantee that the thread launched dies before the next thread is launched. Calling GC.Collect(); does nothing. From my observation this code loses 10 handles every refresh to the task manager at normal refresh.</p>
  
  <p>It doesn't matter what is in the void SendCEcho() function, count to five if you want. When the thread dies there is one handle that does not get cleaned up.</p>
  
  <p>With most programs this doesn't really matter because they do not run for extended periods of time. On some of the programs I've created they need to run for months and months on end.</p>
  
  <p>If you exercise this code over and over, you can eventually leak handles to the point where the windows OS becomes unstable and it will not function. Reboot required.</p>
  
  <p>I did solve the problem by using a thread pool and code like this:</p>
  
  <pre><code>ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadJobMoveStudy));
  </code></pre>
  
  <p>My question though is why is there such a leak in .Net, and why has it existed for so long? Since like 1.0? I could not find the answer here.</p>