5

I created a thread A and decided to abort it from inside the thread A. Is that possible? If so how can I do that?

Thanks for the help!

Piotr Zierhoffer
  • 5,005
  • 1
  • 38
  • 59
Dilshod
  • 3,189
  • 3
  • 36
  • 67

1 Answers1

16

Sure, you can do:

Thread.CurrentThread.Abort();

Arguably aborting the current thread is the only kind of abort that's safe, as it's the only thread you really know about in terms of what it's doing. This is what HttpResponse.End does, for example.

It's not something I would typically recommend - basically it's done because unless you explicitly call Thread.ResetAbort, the ThreadAbortException will propagate up the call stack regardless of catch blocks - it's a sort of "uber-exception" in that sense.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 1
    Okay, I know it's safe. I'd still look askance at any code that used this. Kind of like seeing a `goto` when there are other, cleaner, ways of doing things. In fact, I've called `Thread.Abort` "the `goto` of multithreaded programming." I'm not 100% set against using `Abort` here, just like I'm not 100% set against using `goto`. But . . . – Jim Mischel Sep 26 '13 at 21:16
  • @JimMischel: Yes, it's pretty grim - but at least it's better than aborting a *different* thread. Will edit to make it clear that it's not a recommended strategy. – Jon Skeet Sep 26 '13 at 21:19