6

Can I kill a .NET 4.0 Task object with some method like we could with the old and dangerous Thread.Abort() as discussed in the SO thread (pun intended and save the kittens!)

Kenny

Community
  • 1
  • 1
kenny
  • 21,522
  • 8
  • 49
  • 87
  • duplicated item: http://stackoverflow.com/questions/4359910/is-it-possible-to-abort-a-task-like-aborting-a-thread-thread-abort-method – Kai May 25 '12 at 00:43
  • I know it's dangerous, but can it be done? I guess you're saying no. – kenny May 25 '12 at 00:46
  • @kenny Not without a lot of knowledge about the `Task` - If you knew, in advance, that the `TaskScheduler` being used was using a dedicated thread for the `Task`, you could call `Abort` on that thread. That being said, it'd be a VERY bad idea to design a system that way. – Reed Copsey May 25 '12 at 00:47
  • @ReedCopsey I agree it's bad design, but it's out of my hands. – kenny May 25 '12 at 00:49

1 Answers1

2

The Task class is designed, and intended, to use the new Cooperative Cancellation model of .NET 4 instead of relying on a destructive "abort" style of cancellation.

There is no direct way to cancel a Task (like Thread.Abort(), though that's very bad to use in any case), but there is an entire framework in place to provide the tooling to request that the Task cancel itself.

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • 1
    Yeah, I've seen that CancelationTokenSource and the cooperative method how it should be done, but I have a case where I need to Abort the task (talking to 3rd party hardware API) and I'm willing to go unstable to do it. THanks again!. – kenny May 25 '12 at 00:48
  • @kenny There's no mechanism in `Task` to allow this. – Reed Copsey May 25 '12 at 00:50
  • @kenny, `Thread` is not deprecated, it's just that most of the time, `Task`s are more convenient. – svick May 25 '12 at 06:10
  • 1
    it is possible to provide provide a Task's Thread.Abort as a delegate for the cancellation token: http://stackoverflow.com/a/21715122/571637 – jltrem Feb 12 '14 at 14:27
  • @jltrem That's a very, very bad idea, though. Your aborting a ThreadPool thread, which can lead to very bad behavior in the entire system. – Reed Copsey Feb 12 '14 at 18:26
  • 1
    @ReedCopsey Agreed, which is why I said "don't do this". But the answer to "can I do it?" is "yes". – jltrem Feb 12 '14 at 19:22
  • @jltrem You're actually not cancelling the Task, though -you're killing it's underlying thread (there is a difference, at least in the way I look at it) – Reed Copsey Feb 12 '14 at 19:45
  • This makes no sense. If there's a Task that hangs indefinitely or would hang indefinitely, due to a logic error or something, in production code, such as a processing queue... there MUST be a way to terminate the Task. Otherwise, they would start an operation, continue to accumulate in memory, as more and more start to run forever, with no way to cancel them. Even if the framework could cancel it's scheduled jobs, it couldn't cancel the Task itself. This seems like a huge flaw. Also, Thread.Abort exists. It's a thing. ThreadAbortExceptions are a thing, and they're triggered by ASP.NET recycle. – Triynko Dec 18 '18 at 21:11