title: uwp C# cancel async task and re-run
body: <p>Thanks in advance.
  I want to use a foreach to add items to a `GridView . This is done in an <em>async</em> method.
  I want to retrieve the items from other <em>async</em> methods and display them in a GridView:</p>
  
  <pre><code>public async Task SetGridItems(CancellationToken ct, /* and some items */)
  {
  
      GridItems.clear();
      //get two list of item I want
      var ListA = await getBmethodAsync().AsTask(ct);
      var ListB = await getAmethodAsync().AsTask(ct);
  
      foreach (itemA A in ListA)
      {     
          GridItem i = new GridItem();
  
          //set name
          i.name = A.Name;
  
          //get icon
          i.image = img;
  
          GridItems.Add(i);
      }
  
      foreach (ItemB b in ListB)
      {
           GridItem i = new GridItem();
           i.name = b.Name;
           i.image.SetSource(icon);
  
           GridItems.Add(i);
      }
  }
  </code></pre>
  
  <p>The content is simplified for convenience.</p>
  
  <p>When I run this method in a button click handler:</p>
  
  <pre><code>private async void check_btn2_Click(object sender, RoutedEventArgs e)
  {    
      if (cts != null) { 
          cts.Cancel();
          debug.Text = "cancel a job 4";
      }
  
      cts = new CancellationTokenSource();
      try
      {
         await SetGridItems(ct.Token, /* and some items */).AsAsyncAction().AsTask(cts.Token);
      }
      catch (OperationCanceledException) { debug.Text = "cancel a job"; }
  }
  </code></pre>
  
  <p>Here is the problem:</p>
  
  <p>If I click this button twice (clicking fast):
  - on the first click, the event callback will be called and my task will start to run
  - something will show in the <code>GridView</code> but will not complete (it ends at 80%)
  - on second click, the <code>GridView</code> is cleared as expected, and some new content is loaded but the <code>GriView</code>is only showing the last 20% of the first click task</p>
  
  <p>So, why does the second click <strong>not</strong> cancelling the first task ?</p>
  
  <p>I've searched for a long time on net, nut haven't found anything. Please help and try to give some ideas how to achieve this.</p>
  
  <p>I am not good at English and thanks</p>