36

In the .NET space there has been a lot of interest in things like async, await and TPL in the recent years. A very common issue is that someone expects cancellation to be one of these:

  1. immediate
  2. pre-emptive (as opposed to cooperative)
  3. as easy as the Task.Run(..., cancellationToken) API makes it seem
  4. somehow built into async/await.

We need a canonical answer that explains...

  1. ...that cancellation is cooperative by nature
  2. ...how to achieve it when a cancellation token can be passed
  3. ...how to cancel when the API you are calling is not cooperating (e.g. by isolating and ignoring the still running action)
  4. ...common mistakes such as expecting Task.Run(..., cancellationToken) to always work
  5. ...that Thread.Abort is not a solution although it appears to work.

Subjectively I think we have about 10 questions per week that require a rote answer to explain one of the points above. Here's the latest one. There are a lot of them in total with very few unique issues among them:

enter image description here

I did not find a good canonical that can be used to routinely close off cancellation questions.

Putting this here in case someone is interested in spending maybe 1 hour to create a comprehensive nice canonical answer. The goal should be to come to a place where we routinely close off these questions just like we do with NullReferenceException. The canonical NullReferenceException works excellently for this purpose.

15
  • 24
    I nominate Stephen Cleary, should he choose to accept it :)
    – David L
    Jun 15, 2016 at 16:44
  • 1
    Also if you write a high quality canonical, there's a good chance that Madara will give you a nice bounty.
    – DavidG
    Jun 15, 2016 at 16:45
  • It definitely would need some work as it is super specific right now but I have an answer that covers part of how cancellation tokens are used. If you think that question is a good candidate I can work on improving my answer.
    – user1618236
    Jun 15, 2016 at 17:38
  • Madara appears to be an awesome user. I did not know about his mission to promote excellent content.
    – usr
    Jun 15, 2016 at 19:00
  • @Phaeze I find that answer to be too specific. I have given my idea of question and answer in this meta post as an outline. It really needs to cover everything comprehensively so that we can routinely close questions without much work.
    – usr
    Jun 15, 2016 at 19:01
  • @usr yeah I agree, if the question was a good fit I would put the time in to make the answer more comprehensive. I admittedly knew nothing about cancellation tokens before developing that answer.
    – user1618236
    Jun 15, 2016 at 19:05
  • @Phaeze if you choose to take this on, don't improve this answer. Work on a freshly written question and answer pair that is free of cruft. As the answer stands it covers very little of the material that I proposed anyway. That's why I'm surprised you mention that answer as a candidate.
    – usr
    Jun 15, 2016 at 19:10
  • 2
    @Phaeze if you want feedback on that answer, by calling await on a Task or async method we are actually suspending that thread so it will block any successive code is not accurate. await releases the thread (kind of), it does not suspend it. Not trying to put down your experience but maybe this task is one notch above your current skill?! I can't know for sure.
    – usr
    Jun 15, 2016 at 19:12
  • @usr, fair enough and I do agree that I would need to do some reading to be able to take on this task.
    – user1618236
    Jun 15, 2016 at 19:19
  • 8
    @David L: I look forward to - nay, I await - his contribution of such an answer.
    – BoltClock
    Jun 16, 2016 at 7:13
  • 1
    That can be expanded to thread/process cancellation in general, and even further to suspending/resuming threads in general. That's only the proverbial tip of the ice berg. About a year ago suspend resume was at 2330, now we have over 3100. That's depressing.
    – JensG
    Jun 16, 2016 at 18:05
  • @JensG I think that's a great idea but for another canonical. Maybe you want to propose it on Meta.; I find that Stack Overflow is very weak on creating canonicals. It takes 1000 repeated questions until someone writes up the issue. Such a waste of time.
    – usr
    Jun 16, 2016 at 18:47
  • 2
    Is this something that would be best placed on the new Documentation site? Jun 17, 2016 at 0:18
  • 1
    @volt Only if we can close questions as duplicates of something posted to the Documentation site. Jun 17, 2016 at 7:08
  • If written, it would also be nice to make people aware that often tasks or async is misused in place of an event driven approach. re: msdn.microsoft.com/en-us/library/… (I would also like to see someone like Stephen Cleary write this)
    – Travis J
    Jun 29, 2016 at 23:42

1 Answer 1

-5

I like to redirect people to this article: https://blogs.msdn.microsoft.com/andrewarnottms/2014/03/19/recommended-patterns-for-cancellationtoken/

1
  • 3
    Redirecting people off-site isn't what we want to do, nor is it what this question is asking for.
    – Daedalus
    Jun 29, 2016 at 22:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .