How to cancel a running task?
问题 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! 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(() => { //...my program }, token)); private void Form_KeyDown(object sender, KeyEventArgs e) { if (e