title: C# thread pool limiting threads
body: <p>Alright...I've given the site a fair search and have read over many posts about this topic. I found this question: <a href="https://stackoverflow.com/questions/435668/code-for-a-simple-thread-pool-in-c">Code for a simple thread pool in C#</a> especially helpful.</p>
  
  <p>However, as it always seems, what I need varies slightly.</p>
  
  <p>I have looked over the MSDN example and adapted it to my needs somewhat. The example I refer to is here: <a href="http://msdn.microsoft.com/en-us/library/3dasc8as(VS.80,printer).aspx" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/library/3dasc8as(VS.80,printer).aspx</a></p>
  
  <p>My issue is this. I have a fairly simple set of code that loads a web page via the <code>HttpWebRequest</code> and <code>WebResponse</code> classes and reads the results via a <code>Stream</code>. I fire off this method in a thread as it will need to executed many times. The method itself is pretty short, but the number of times it needs to be fired (with varied data for each time) varies. It can be anywhere from 1 to 200.</p>
  
  <p>Everything I've read seems to indicate the <code>ThreadPool</code> class being the prime candidate. Here is what things get tricky. I might need to fire off this thing say 100 times, but I can only have 3 threads at most running (for this particular task). </p>
  
  <p>I've tried setting the <code>MaxThreads</code> on the <code>ThreadPool</code> via: </p>
  
  <pre><code>ThreadPool.SetMaxThreads(3, 3);
  </code></pre>
  
  <p>I'm not entirely convinced this approach is working. Furthermore, I don't want to clobber other web sites or programs running on the system this will be running on. So, by limiting the # of threads on the <code>ThreadPool</code>, can I be certain that this pertains to my code and my threads only?</p>
  
  <p>The MSDN example uses the event drive approach and calls <code>WaitHandle.WaitAll(doneEvents);</code> which is how I'm doing this. </p>
  
  <p>So the heart of my question is, how does one ensure or specify a maximum number of threads that can be run for their code, but have the code keep running more threads as the previous ones finish up until some arbitrary point? Am I tackling this the right way?</p>
  
  <p>Sincerely,</p>
  
  <p>Jason</p>
  
  <hr>
  
  <p>Okay, I've added a semaphore approach and completely removed the <code>ThreadPool</code> code. It seems simple enough. I got my info from: <a href="http://www.albahari.com/threading/part2.aspx" rel="nofollow noreferrer">http://www.albahari.com/threading/part2.aspx</a></p>
  
  <p>It's this example that showed me how:</p>
  
  <p>[text below here is a copy/paste from the site]</p>
  
  <p>A <code>Semaphore</code> with a capacity of one is similar to a <code>Mutex</code> or <code>lock</code>, except that the <code>Semaphore</code> has no "owner" – it's thread-agnostic. Any thread can call <code>Release</code> on a <code>Semaphore</code>, while with <code>Mutex</code> and <code>lock</code>, only the thread that obtained the resource can release it.</p>
  
  <p>In this following example, ten threads execute a loop with a <code>Sleep</code> statement in the middle. A <code>Semaphore</code> ensures that not more than three threads can execute that <code>Sleep</code> statement at once: </p>
  
  <pre><code>class SemaphoreTest
  {
      static Semaphore s = new Semaphore(3, 3);  // Available=3; Capacity=3
  
      static void Main()
      {
          for (int i = 0; i &lt; 10; i++)
              new Thread(Go).Start();
      }
  
      static void Go()
      {
          while (true)
          {
              s.WaitOne();
  
              Thread.Sleep(100);   // Only 3 threads can get here at once
  
              s.Release();
          }
      }
  }
  </code></pre>