title: ThreadPool and Memory (BTStackServer) exception - .NET
body: <p>In continuation with my <a href="https://stackoverflow.com/questions/9675231/threadpool-and-sending-emails">problem here</a>, I created a test app that uses <code>ThreadPool</code> threads to send emails. Here's the code I am using.</p>
  
  <pre><code>        protected void btnSend_Click(object sender, EventArgs e)
          {
              EmailInfo em = new EmailInfo { Body = "Test", FromEmail = "test@test.com", Subject = "Test Email", To = "test@test.com" };
  //txtNumEmails is a textbox where I can enter number of emails to send
              for (int i = 0; i &lt; Convert.ToInt32(this.txtNumEmails.Text); i++)
              {
                  bool bResult = ThreadPool.QueueUserWorkItem(new WaitCallback(EmailNow), em);
              }
  
          }
  
  
          public void EmailNow(object emailInfo) // Email Info object
          {
              EmailInfo em = emailInfo as EmailInfo;
  
              SmtpClient client = new SmtpClient("localhost");
              client.UseDefaultCredentials = true; 
  
              client.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
              client.PickupDirectoryLocation = @"C:\temp\testemails\";
  
              MailMessage m = new MailMessage();
              m.To.Add(new MailAddress(em.To));
              m.Body = em.Body;
              m.IsBodyHtml = false;
              m.From = new MailAddress(em.FromEmail);
              m.Subject = em.Subject;
  
              client.Send(m);
          }
  </code></pre>
  
  <p>For smaller numbers (10k, 50k) they work great, but once I increase the number to 200k (which is my target), I got this exception:</p>
  
  <p><img src="https://i.stack.imgur.com/Q5p7v.jpg" alt="enter image description here"></p>
  
  <p>It created about 186k emails before it threw this exception.</p>
  
  <p>I am assuming this is not the exception caused due to lack of disk space (since i am storing all the emails locally in <code>C:\temp\testemails</code> but because it was low in RAM(?). As suggested by a <a href="https://stackoverflow.com/a/9677202/102533">this user</a>, I used semaphores to limit them to 10k, but still ran in to the same issue. Here's the code for the one with semaphores.</p>
  
  <pre><code>    protected void Button1_Click(object sender, EventArgs e)
      {
          EmailInfo em = new EmailInfo { Body = "Test", FromEmail = "test@test.com", Subject = "Test Email", To = "test@test.com" };
          var semaphore = new SemaphoreSlim(10000, 10000);
  
          for (int i = 0; i &lt; Convert.ToInt32(this.txtNumEmails.Text); i++)
          {
              semaphore.Wait();
              bool bResult = ThreadPool.QueueUserWorkItem( (state) =&gt; { 
                  try 
                  {           
                      EmailNow(em);
                  }
                  catch (Exception)
                  {
  
                      throw;
                  }
                  finally
                  {
                               semaphore.Release();
                  }
              },  null);
          }
      }
  </code></pre>
  
  <p>This one threw an exception as well but I see all 200k emails in the folder. I can definitely use <code>try catch</code> and gracefully exit if this exception occurs, but how would I prevent this from happening in the first place.</p>