title: async task cancellation c# xamarin
body: <p>I have a functionality of search users. I have provided a textview and on that textview changed method I'm firing a method to get data from web server. But I'm facing problem when user types letter, because all the api hits done in async task. Service should be hit after 100 milli-sec of wait, means if user types a letter "a" then doesn't type for 100 milli-sec then We have to hit the service. But if user types  "a" then "b" then "c", so one service should be hit for "abc", not for all.</p>
  
  <p>I followed the official link, but it doesn't help me
  <a href="https://msdn.microsoft.com/en-us/library/jj155759.aspx">https://msdn.microsoft.com/en-us/library/jj155759.aspx</a></p>
  
  <p>So basically here is my code</p>
  
  <pre><code>textview.TextChange+= (sender,e) =&gt;{
    CancellationTokenSource cts = new CancellationTokenSource();
        await Task.Delay(500); 
        // here some where I have to pass cancel token
        var lst = await APIClient.Instance.GetUserSearch("/user/get?searchTerm=" + newText, "application/json",cts);
        if (lst != null &amp;&amp; lst.Count &gt; 0){ 
          lstSearch.AddRange(lst);
        }
    }
  </code></pre>
  
  <p>Here is my method to GetUser</p>
  
  <pre><code> public async Task&lt;JResponse&gt; GetUserSearch&lt;JResponse&gt;(string uri, string contentType,CancellationToken cts)
      {
          try
          {
              Console.Error.WriteLine("{0}", RestServiceBaseAddress + uri);
  
              string url = string.Format("{0}{1}", RestServiceBaseAddress, uri); 
  
              var request = (HttpWebRequest)WebRequest.Create(url);
              request.ContentType = contentType;
              if (Utility.CurrentUser != null &amp;&amp; !string.IsNullOrWhiteSpace(Utility.CurrentUser.AuthToken))
              {
                  request.Headers.Add("api_key", Utility.CurrentUser.AuthToken);
              }
              request.Method = "POST";
  
              var payload = body.ToString();
  
              request.ContentLength = payload.Length;
  
              byte[] byteArray = Encoding.UTF8.GetBytes(body.ToString());
              request.ContentLength = byteArray.Length;
  
              using (var stream = await request.GetRequestStreamAsync())
              {
                  stream.Write(byteArray, 0, byteArray.Length);
                  stream.Close();
              }
              using (var webResponse = await request.GetResponseAsync())
              {
                  var response = (HttpWebResponse)webResponse;
                  using (var reader1 = new StreamReader(response.GetResponseStream()))
                  {
                      Console.WriteLine("Finished : {0}", uri);
                      var responseStr = reader1.ReadToEnd();  
                      var responseObj = JsonConvert.DeserializeObject&lt;JResponse&gt;(
                          responseStr,
                          new JsonSerializerSettings()
                          {
                              MissingMemberHandling = MissingMemberHandling.Ignore,
                              NullValueHandling = NullValueHandling.Ignore
                          });
                      return responseObj;
                  }
              }
          }
          catch (System.Exception ex)
          {
              Utility.ExceptionHandler("APIClient", "ProcessRequestAsync", ex);
          }
  
          return default(JResponse);
      }
  </code></pre>