1

Whilst searching the reason for a await Task.Run( ... ); not being interrupted when the CancellationToken was cancelled, I've found this SO post where I've learned that it doesn't work as straight forward as it seemed in the beginning.

Having that in mind, I could I force a stop / cancel in a given task with a CancellationToken?

In the same post, I've read that in some cases, an if checking if the given CancellationToken has been cancelled would make the effect I'm searching for, the problem is: I don't have a cycle of any kind in there and - the main reason - I'm only calling a blocking method inside the Task.Run( ... ); (hence the fact of using a Task to not block the UI).

Other problem with this is that that Task is inside a click event, so the user may click again and, due to the Task not being cancelled, I'm afraid that a variable might get caught in the middle, screwing up the app.

Basically, this is what is happening:

  1. User gets there.
  2. User inputs the requested info.
  3. An redirect occurs.
  4. The user is back.
  5. The task starts running. The user can cancel it anytime.

If the user waits for the operation to finish

  1. The task ran to completion. The result is stored into a var.
  2. The var is processed.
  3. The result is shown to the user.

If the user does not wait for the operation to finish and makes the request again

  1. The user cancels the operation.
  2. The user goes trough the steps 1, 2, 3 and 4 again.
  3. The task starts running. The user can cancel it anytime. There are now 2 task running the same method waiting for a result.
  4. From now on, the first task might return the result whilst the second task, the one that should get the result, might return other value other than the expected one. ( I might be wrong here )
  5. The end of the world is near...

How should I manage this? I'm only using the Task.Run( ... ) to avoid the UI being blocked.

Community
  • 1
  • 1
auhmaan
  • 726
  • 1
  • 8
  • 28
  • 1
    The method actually doing the work needs to support cancellation, if you want to be able to cancel it. If it doesn't support cancellation, then you won't be able to cancel it. – Servy Jun 21 '16 at 17:44
  • So, basically, I don't have where to go to fix this, right? Well, either way, I've tested this twice and the `Task` seems to be cancelled _right on time_. Maybe I don't need it... Maybe... – auhmaan Jun 21 '16 at 17:48
  • 1
    You absolutely have a way to fix it, and that's to make the method that actually does the work support cancellation. If it doesn't, the best you can do is have the program keep executing even though the operation is still running. – Servy Jun 21 '16 at 17:49
  • This question reminds me of this post on meta: [The topic area “.NET async/await/TPL” needs a canonical answer on cooperative cancellation](http://meta.stackoverflow.com/questions/326188/the-topic-area-net-async-await-tpl-needs-a-canonical-answer-on-cooperative-ca). – sstan Jun 21 '16 at 17:54
  • @sstan I was aware of that, just wanted to be sure if a solution could be achieved other than _waiting_ for it. – auhmaan Jun 21 '16 at 17:57

0 Answers0