title: c# check if thread/background task has finished or needs aborting
body: <p>I have a fairly simple requirement for my console/windows service (I can run it either mode) application:</p>
  
  <ol>
  <li>Get a list of items to work on from the database</li>
  <li>Start a method in the background (only one, never a need for more) to process one item</li>
  <li>Check it has finished or needs terminating (via a sql lookup)</li>
  <li>Repeat 2 when finished/aborted for the next</li>
  <li>When no more,  sleep a little and repeat 1</li>
  </ol>
  
  <p>I'm fairly new to c#/.net and there are various threading systems I can see. Which is better for this case, a thread or a task?</p>
  
  <p>In the case of a thread I assume it's something like (this rough code) for each item to process:</p>
  
  <pre><code>Thread thread = new MyThread(new ThreadStart(this.SomeFunction));
  thread.Start();
  while(!finished) {
    if (!thread.IsAlive())
      finished=true; 
    else {
       //check database for early termination of job
       terminate=SomeChdck();
       if(terminate) { thread.Abort(); finished=true;}
  
    }
  }
  </code></pre>
  
  <p>//return back and repeat</p>
  
  <p>or in the case of Task it's something like (again, roughly done, and pinched from the web):</p>
  
  <pre><code>var tokenSource = new CancellationTokenSource();
  var token = tokenSource.Token;
  Task task = Task.Factory.StartNew(this.SomeFunction, token);
  while(!finished) {
    if (task.IsCompleted)
      finished=true;
    else {
       //check database for early termination of job
       terminate=SoneChdck();
       if(terminate) { tokenSource.Cancel(); finished=true;}
    }
  }
  </code></pre>
  
  <p>//return back and repeat</p>
  
  <p>Is there a difference in the approach of these (assuming they will both work that is), and I read somewhere that Thread.Abort() was deprecated but there's no mention at the documentation.</p>
  
  <p>thanks.</p>