4

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.KeyCode == Keys.Escape)
  {
    tokenSource.Cancel();
  }
}
PEYMAN
  • 55
  • 1
  • 1
  • 5
  • Possible duplicate of [How do I abort/cancel TPL Tasks?](https://stackoverflow.com/questions/4783865/how-do-i-abort-cancel-tpl-tasks) – SᴇM May 08 '18 at 11:10
  • 2
    `please help me this code is not working!` What do you mean by 'not working'? – mjwills May 08 '18 at 11:10

1 Answers1

9

You are doing all ok except the fact that you need to stop your code from running inside your method. By passing cancellation token to Task.Factory.StartNew you are not aborting the task. Let me quote Stephen Toub:

Passing a token into StartNew associates the token with the Task. This has two primary benefits: 1) If the token has cancellation requested prior to the Task starting to execute, the Task won't execute. Rather than transitioning to Running, it'll immediately transition to Canceled. This avoids the costs of running the task if it would just be canceled while running anyway. 2) If the body of the task is also monitoring the cancellation token and throws an OperationCanceledException containing that token (which is what ThrowIfCancellationRequested does), then when the task sees that OCE, it checks whether the OCE's token matches the Task's token. If it does, that exception is viewed as an acknowledgement of cooperative cancellation and the Task transitions to the Canceled state (rather than the Faulted state).

You need to manually check if your token was cancelled and throw operation canceled exception, something along these lines:

 private async void Search_Button_ClickAsync(object sender, EventArgs e)
    {
      cToken = cTokenSource.Token;
      await (Task.Factory.StartNew(() =>
               {

               for(int i=0;i<yourtaskcount;i++)
               { 
                 cToken.ThrowIfCancellationRequested();

                  //long work
               cToken));
    }
    private void Form_KeyDown(object sender, KeyEventArgs e)
    {
      if (e.KeyCode == Keys.Escape)
      {
        tokenSource.Cancel();
      }
    }
MistyK
  • 6,055
  • 2
  • 42
  • 76
  • 1
    thanks dear friend but i need to cancel immediately after click "escape" – PEYMAN May 08 '18 at 11:23
  • @PEYMAN It depends how your business logic look like and how often you can check for cancellation. You can also abort the task but it's not recommended. Check here: https://stackoverflow.com/questions/4359910/is-it-possible-to-abort-a-task-like-aborting-a-thread-thread-abort-method You can also cancel the task, and let it be cancelled after some time in the background. Your UI can pretend that it's already cancelled enable buttons/allow next run to be started. You need to be careful about thread safety though. – MistyK May 08 '18 at 11:25
  • @Default if i use ThrowIfCancellationRequested then task cancel immediately? – PEYMAN May 08 '18 at 11:29
  • 1
    @PEYMAN yes it does but it's equivalent to the previous solution but shorter – MistyK May 08 '18 at 11:33
  • @Default can you give me code of my example program with your solution? – PEYMAN May 08 '18 at 11:48