title: How does a .Net ThreadPool thread created?
body: <p>I am trying to understand how TheadPool works. A simple console app like this:</p>
  
  <pre><code>public static void Main()
  {
      foreach (ProcessThread thread in Process.GetCurrentProcess().Threads)
      {
          Console.WriteLine($"thread.ThreadState: {thread.ThreadState}");
      }    
      Console.ReadLine();
  }
  </code></pre>
  
  <p>The output is:</p>
  
  <pre><code>thread.ThreadState: Running  
  thread.ThreadState: Wait  
  thread.ThreadState: Wait  
  thread.ThreadState: Wait  
  thread.ThreadState: Wait 
  </code></pre>
  
  <p>So there are 5 threads, 1 running, 4 waiting. My questions are:  </p>
  
  <ol>
  <li><p>Where do these threads come from? <s>I guess these 4 waiting threads are ThreadPool threads. They must be created before entering my main method.</s> Can you point me to the .Net source code that creating those threads?  </p></li>
  <li><p>I know we can post a task to ThreadPool using ThreadPool.QueueUserWorkItem, but how does the ThreadPool pick up a task/delegate? I guess there is some thing like the message pump for WinForm UI thread, is there one background thread constantly checking if there is any new task? Also can I see the source code?</p></li>
  </ol>
  
  <p><strong>Edit:</strong> 
  Thanks to Thomas, so my assumptions/imaginations about ThreadPool are totally wrong. I will open another question with more appropriate examples.</p>