title: How to Kill or Abort Task.Run or Task.Factory.StartNew() in C#
body: <p>I have situation where i have to Use <code>Task.Run</code> In my <code>ForEach</code> loop</p>
  
  <p><strong>Requirement:</strong>
  I'm going to be forced to manually kill thread
  I have button where i can start and stop this <code>Thread</code> or <code>Task.Run</code> in For loop.</p>
  
  <p><strong>Problem</strong>
  My problem is when i start the Task.Run method Its running but when i try to stop with using <code>CancellationTokenSource</code> or <code>runningTaskThread.Abort();</code> it will not kill. its just stop when i start new Task.Run at that time it run with old thread so it become multiple thread every start process.</p>
  
  <p><strong>Code:</strong></p>
  
  <p><strong>Below is my code for start Thread</strong></p>
  
  <pre><code> var messages = rootObject.MultiQData.Messages.Where(m =&gt; m.TimeStamp &gt; DateTime.Now).OrderBy(x =&gt; x.TimeStamp).ToList();
                                  //Simulate MultiQ file in BackGroud 
                                  if (messages.Count &gt; 0)
                                  {
                                      cancellationTokenSource = new CancellationTokenSource();
                                      cancellationToken = cancellationTokenSource.Token;
                                      Task.Factory.StartNew(
                                          () =&gt;
                                          {
                                              runningTaskThread = Thread.CurrentThread;
                                              messages.ForEach(
                                                  m =&gt; SetUpTimer(m, rootObject.MultiQData.Connection.FleetNo));
                                          }, cancellationToken);
                                  }
  </code></pre>
  
  <p><strong>For stop Task.Run</strong> </p>
  
  <pre><code> if (cancellationTokenSource != null)
              {
                  if (cancellationToken.IsCancellationRequested)
                      return;
                  else
                      cancellationTokenSource.Cancel();
              }
  </code></pre>
  
  <p>I have also use <code>Thread</code> with <code>Thread.Abort</code> but it is not working </p>
  
  <p>Please Help to solve this issue</p>