title: How to cancel a running task?
body: <p>I want to cancel a running task (when the users presses the escape key).
  when i click on "escape" key Form_KeyDown run but doesn't cancel task!</p>
  
  <pre><code>CancellationTokenSource tokenSource = new CancellationTokenSource();
  CancellationToken token=new CancellationToken();
  
  private async void Search_Button_ClickAsync(object sender, EventArgs e)
  {
    token = tokenSource.Token;
    await (Task.Factory.StartNew(() =&gt;
             {
               //...my program
             },
             token));
  
  private void Form_KeyDown(object sender, KeyEventArgs e)
  {
    if (e.KeyCode == Keys.Escape)
    {
      tokenSource.Cancel();
    }
  }
  </code></pre>