title: Preventing threads from processing further once call to stop is being made
body: <p>I have a windows service which will start and stop the execution of some process that is being done with the held of Threads.</p>
  
  <p>I have two classes as follows:</p>
  
  <pre><code>public class PerformTask
  {
      Thread _thread = null;
      public void StartTask()
      {
          _thread = new Thread(new ThreadStart(DoSomeWork));
          _thread.Start();
      }
  
      public void DoSomeWork()
      {
          // Do Some Work
          _thread = null;
      }
  
      public void Abort()
      {
          if (_thread != null)
          {
              try
              {
                  _thread.Abort();
              }
              catch (ThreadAbortException) {}
          }
      }
  }
  
  public class Engine
  {
      List&lt;PerformTask&gt; _task = new List&lt;PerformTask&gt;();
      public void Start()
      {
          var task = new PerformTask();
          _task.Add(task);
          // Add task to the timed action queue
          _actionQueue.Add(s =&gt; task.StartTask(), TimeSpan.FromSeconds(10));
      }
  
      public void Stop()
      {
          _task.ForEach(task =&gt; task.Abort());
          _task.Clear();
          _actionQueue.Stop();
          _actionQueue.Clear();
      }
  }
  </code></pre>
  
  <p>The _actionQueue is a custom defined source code developed to perform a specified action at a recurring time interval specified. All the actions are kept in queue and invoked at the specified time interval.</p>
  
  <p>Now, the Windows service's OnStart and OnStop method would call Engine class' Start and Stop method respectively.</p>
  
  <p>What I want is when the windows service is stopped, all the threads that are running should stop their processing/execution.</p>
  
  <p>But, what is happening here is as new thread instance is being created in I have a windows service which will start and stop the execution of some process that is being done with the held of Threads.</p>
  
  <p>I have two classes as follows:</p>
  
  <pre><code>public class PerformTask
  {
      Thread _thread = null;
      public void StartTask()
      {
          _thread = new Thread(new ThreadStart(DoSomeWork));
          _thread.Start();
      }
  
      public void DoSomeWork()
      {
          // Do Some Work
          _thread = null;
      }
  
      public void Abort()
      {
          if (_thread != null)
          {
              try
              {
                  _thread.Abort();
              }
              catch (ThreadAbortException) {}
          }
      }
  }
  
  public class Engine
  {
      List&lt;PerformTask&gt; _task = new List&lt;PerformTask&gt;();
      ActionQueue _actionQueue = new ActionQueue();
      public void Start()
      {
          foreach(.....)
          {
              var task = new PerformTask();
              _task.Add(task);
              // Add task to the timed action queue
              _actionQueue.Add(s =&gt; task.StartTask(), TimeSpan.FromSeconds(10));
          }
          _actionQueue.Start();
      }
  
      public void Stop()
      {
          _task.ForEach(task =&gt; task.Abort());
          _task.Clear();
          _actionQueue.Stop();
          _actionQueue.Clear();
      }
  }
  </code></pre>
  
  <p>The ActionQueue is a custom defined source code developed to perform a specified action at a recurring time interval specified. All the actions are kept in queue and invoked at the specified time interval.</p>
  
  <p>Now, the Windows service's OnStart and OnStop method would call Engine class' Start and Stop method respectively.</p>
  
  <p>What I want is when the windows service is stopped, all the threads that are running should stop their processing/execution.</p>
  
  <p>But, what is happening here is as new thread instance is being created in StartTask method, when I call the
      _task.ForEach(task => task.Abort())
  I do not have the correct instance of Thread, that is all the instance of
      _thread = new Thread(....);
  is not being accessed, as there would a multiple queues for the same PerformTask class.</p>
  
  <p>Note: I cannot make changes to the ActionQueue.</p>
  
  <ol>
  <li><p>Is Abort method a correct way of stopping the threads?</p></li>
  <li><p>How can I stop all the threads (including all the instances of Thread class created by the source code)?</p></li>
  </ol>