title: How to cancel Task so it stops
body: <p>I am using a Task to read some data from the Database etc. Let's assume I can't change the Dataaccess-API/-Layer.</p>
  
  <p>This dataaccess might sometimes need some time (network-traffic etc.). It will be loaded everey time a user changes a selected item or changes the filter that shows a subset of the aviable data.</p>
  
  <p>At the end I have a little example of my Task-Start-Method.</p>
  
  <p><strong>My Question is:</strong> If the user changes the filter/selection while the task is still running, how can I stop it from running? When using the cancellation token it will finish (as I am not using a "big" loop in the task, I can't just check every iteration for <code>.IsCancelled</code>.</p>
  
  <p>My idea was to use the return type of the Task to fill the <code>SelectableVolts</code> and check the returning Task on <code>IsCancelled</code> before assigning the new value. But how to do this for an async Task?</p>
  
  <pre><code>// added code at the bottom of this question
  </code></pre>
  
  <p><strong>UPDATE:</strong> After getting comments like "I am not totally sure what your question is asking for, but this should help you get a feel for some avalible options." I will try to clarify my problem a little bit. At least I hope so ;)</p>
  
  <ol>
  <li>User selects object in datagrid  </li>
  <li>ViewModel needs data and asks method/class/foo for data.  </li>
  <li>Task(A) is started   </li>
  <li>Task(A) is still working. User selects different object/row.  </li>
  <li>Steps 1, 2 and 3 are repeated. So Task(A) should be cancelled/stopped and a new Task(B) starts.  </li>
  <li>When Task(B) is finished its data should be displayed. In no way Task(A)s data should be made available. </li>
  </ol>
  
  <p>So the question is: How to achieve steps 5 and 6 in a correct way. </p>
  
  <p><strong>Full Code:</strong></p>
  
  <pre><code>    private CancellationToken cancelTokenOfFilterTask;
      private CancellationTokenSource cancelTokenSourceOfFilterTask;
  
  private void FilterVolts()
  {
      if (IsApplicationWorking)
      {
          cancelTokenSourceOfFilterTask.Cancel();
      }
  
      // Prepare CancelToken
      cancelTokenSourceOfFilterTask = new CancellationTokenSource();
      cancelTokenOfFilterTask = cancelTokenSourceOfFilterTask.Token;
  
      IsApplicationWorking = true;
      if (SelectableVolts != null &amp;&amp; SelectableVolts.Count &gt;= 0)
      {
          VoltThatIsSelected = null;
          SelectableVolts.Clear();
      }
  
      Task voltLoadTask = null;
      voltLoadTask = Task.Factory.StartNew&lt;List&lt;SelectableVoltModel&gt;&gt;(() =&gt; {
         VoltsLoader loader = new VoltsLoader();
         Thread.Sleep(2000);
         var listOfVolts = loader.LoadVoltsOnFilter(_sourceOfCachableData, ChosenFilter);
              return listOfVehicles;
          }, cancelTokenOfFilterTask).ContinueWith(listSwitcher =&gt;
          {
              switch (listSwitcher.Status)
              {
                  case TaskStatus.RanToCompletion:
                      SelectableVolts = new ObservableCollection&lt;SelectableVoltsModel&gt;(listSwitcher.Result);
                      IsApplicationWorking = false;
                      break;
                  case TaskStatus.Canceled:
                      Console.WriteLine("Cancellation seen"); // Gets never called
                      break;           
                  default:
                      break;
              }
          });
      }
  </code></pre>
  
  <p>Somehow when I call this Method more than once, all will run in <code>TaskStatus.RanTocompletion</code> how can this be possible? Am I doing someting wrong with the cancel-token?</p>