title: How best to dispose a thread that is sleeping because of AutoResetEvent.WaitOne()
body: <p>I have a windows service that sends email in a one of 5 threads (done to increase the speed the service can send email):</p>
  
  <pre><code>private AutoResetEvent block;
  private ThreadedQueue&lt;Message&gt; messageQueue;        
  
  private void DoSend()
  {
      try
      {   
          while(!this.disposing)
          {
              this.block.WaitOne();
  
              Message message = null; 
              if (this.messageQueue.TryDequeue(out message))
              {                       
                  this.block.Set();
              }                   
  
              if(message != null)
              {
                  this.Send(message);                 
              }
          }
      }
      catch(Exception ex)
      {
          // Log
      }
  }
  </code></pre>
  
  <p>I have a <code>Queue</code> method that adds one or more new message to the messageQueue and calls <code>block.Set()</code> so that one of the 5 threads can send the message.  When one of the threads is allowed to run, so long as there are messages in the queue, <code>block.Set()</code> is called so that the next message can be de-queued and another of 5 threads will work to send it.  And so on, until the queue is empty.  This all works OK.</p>
  
  <p>However when I dispose my object, I set the disposing variable and then for each thread:</p>
  
  <pre><code>if(thread.ThreadState == ThreadState.Running)
  {
      thread.Join();
  }
  else if(thread.ThreadState == ThreadState.WaitSleepJoin)
  {
      thread.Abort();
  }
  </code></pre>
  
  <p>Most of the time, the threads are sleeping due to the <code>block.WaitOne</code> and so the above code aborts the thread.  However this causes thread abort exceptions to be logged.  I could catch thread abort exceptions separately to other exceptions and choose not to log, but it doesn't seem very clean.</p>
  
  <p>What is the best way to clean up these threads without causing this excess logging?</p>
  
  <p>UPDATE:</p>
  
  <p>I've changed the above to:</p>
  
  <pre><code>private ManualResetEvent block;
  private ThreadedQueue&lt;Message&gt; messageQueue;        
  
  private void DoSend()
  {
      try
      {   
          while(!this.disposing)
          {
              this.block.WaitOne();
  
              Message message = null; 
              if (!this.messageQueue.TryDequeue(out message) &amp;&amp; !this.disposing)
              {                       
                  // There's nothing else to send for now to block the sending threads
                  // unless we're disposing as we want the other threads to exit too
                  this.block.Reset();
              }                   
  
              if(message != null)
              {
                  this.Send(message);                 
              }
          }
      }
      catch(Exception ex)
      {
          // Log
      }
  }
  
  public void Dispose()
  {           
      this.disposing = true;
      this.block.Set();           
      foreach(Thread thread in this.sendingThreads) {             
          thread.Join();
      }
      this.block.Dispose();
      this.sendingThreads = null;
  }
  </code></pre>
  
  <p>Thanks for the help.</p>