title: How cancel Async Call in Windows Phone?
body: <p>I have a list wich is loaded with elements each time the user make a research...These elements contain an Icon which is dowloaded with an async method GetByteArrayAsync of the HttpClient object. <BR>I have an issue when the user make a second research while the icon of the first list are still downloading.Because the list of elements is changing while Icon downloads are processing on each element of  the first list.<Br> So my guess is that I need to cancel these requests each time the user proceed to a new research...Ive readen some stuuf on Task.run and CancellationTokenSource but I can't find really helpful example for my case so here is my code...<Br>Hope you can help me with that ...Thank you</p>
  
     <pre class="lang-cs prettyprint-override"><code>public static async Task&lt;byte[]&gt; DownloadElementFile(BdeskElement bdeskElement)
  {
      //create and send the request
  
      DataRequest requesteur = new DataRequest();
  
      byte[] encryptedByte = await requesteur.GetBytesAsync(dataRequestParam);
  
      return encryptedByte;
  }
  
  
  public async Task&lt;Byte[]&gt; GetBytesAsync(DataRequestParam datarequesparam)
  {
      var handler = new HttpClientHandler { Credentials = new NetworkCredential(datarequesparam.AuthentificationLogin, datarequesparam.AuthentificationPassword, "bt0d0000") };
      HttpClient httpClient = new HttpClient(handler);
  
      try
      {
          byte[] BytesReceived = await httpClient.GetByteArrayAsync(datarequesparam.TargetUri);
  
          if (BytesReceived.Length &gt; 0)
          {
              return BytesReceived;
          }
  
          else
          {
              return null;
  
          }
      }
      catch (WebException)
      {
          throw new MyException(MyExceptionsMessages.Webexception);
      }
  </code></pre>
  
  <p>}</p>
  
  <p><strong>EDIT</strong></p>
  
  <pre class="lang-cs prettyprint-override"><code>public async Task&lt;Byte[]&gt; GetBytesAsync(DataRequestParam datarequesparam)
          {
              var handler = new HttpClientHandler { Credentials = new NetworkCredential(datarequesparam.AuthentificationLogin, datarequesparam.AuthentificationPassword, "bt0d0000") };
              HttpClient httpClient = new HttpClient(handler);
  
              try
              {
                  cts = new CancellationTokenSource();
  
                  HttpResponseMessage reponse = await httpClient.GetAsync(datarequesparam.TargetUri,cts.Token);
  
                 if (reponse.StatusCode == HttpStatusCode.OK)
                 {
                     byte[] BytesReceived = reponse.Content.ReadAsByteArrayAsync().Result;
  
                     if (BytesReceived.Length &gt; 0)
                     {
                         return BytesReceived;
                     }
  
                     else
                     {
                         return null;
  
                     }
                 }
  
                 else
                 {
                     return null;
                 }
  
              }
              catch (WebException)
              {
                  throw new MyException(MyExceptionsMessages.Webexception);
              }
                catch(OperationCanceledException)
              {
                    throw new OperationCanceledException();
              }
  </code></pre>
  
  <p><strong>EDIT2</strong>
  I need to cancel this funntion when the user make a new research and the list "listBoxGetDocsLibs" changed.</p>
  
  <pre class="lang-cs prettyprint-override"><code> private async void LoadIconDocLibs()
          {
  
              foreach (var doclib in listBoxGetDocsLibs)//ERROR HERE COLLECTION HAS CHANGED
              {
  
                  doclib.Icon = new BitmapImage();
  
  
                      try
                      {
  
                          byte[] Icon = await ServerFunctions.GetDocLibsIcon(doclib);
  
                          if (Icon != null)
                          {
                              {
  
                                  var ms = new MemoryStream(Icon);
  
                                  BitmapImage photo = new BitmapImage();
                                  photo.DecodePixelHeight = 64;
                                  photo.DecodePixelWidth = 92;
                                  photo.SetSource(ms);
                                  doclib.Icon = photo;
                              }
                          }
                      }
                      catch(OperationCanceledException)
                      {
  
                      }
                  }
  
  
          }
  </code></pre>