6

MSDN on migrating legacy multithreaded applications (from this page on exception handling in threads):

In general, the change will expose previously unrecognized programming problems so that they can be fixed. In some cases, however, programmers might have taken advantage of the runtime backstop, for example to terminate threads. Depending on the situation, they should consider one of the following migration strategies:

Restructure the code so the thread exits gracefully when a signal is received.

Use the Thread.Abort method to abort the thread.

If a thread must to be stopped so that process termination can proceed, make the thread a background thread so that it is automatically terminated on process exit.

In all cases, the strategy should follow the design guidelines for exceptions. See Design Guidelines for Exceptions.

This suggests that using Thread.Abort is an appropriate way to terminate a thread. Has something changed while I wasn't looking? The last I'd heard was this could cause unexpected behaviours so shouldn't be used.

Phil Gan
  • 2,813
  • 2
  • 29
  • 38
  • Check answers on this post: http://stackoverflow.com/questions/6763015/does-some-event-like-thread-onaborting-exist – Otiel Aug 16 '11 at 16:09
  • possible duplicate of [Timeout Pattern - How bad is Thread.Abort really?](http://stackoverflow.com/questions/710070/timeout-pattern-how-bad-is-thread-abort-really) – H H Aug 16 '11 at 16:32

3 Answers3

5

Thread.Abort is a lot safer than it used to be for the following reasons.

  • The runtime will defer aborts while execution is in unmanaged code.
  • The abort will allow finally blocks to execute.

However, there is still a problem with exactly when the ThreadAbortException gets injected. Consider this code.

public class Example
{
  private DateTime value = DateTime.MinValue;

  public void DoSomething()
  {
    try
    {
      value = DateTime.UtcNow;
    }
    finally
    {
    }
  }
}

If this code were running on a 32-bit platform the value variable could be corrupted if Thread.Abort was called and the ThreadAbortException were injected in the middle of the write to value. Since DateTime is 8 bytes the write has to take place using more than one instruction.

It is possible to guard against this by placing critical code in a finally block and by using Constrained Execution Regions, but it would be incredibly difficult to get right for all but the simplest types your define. And even then you cannot just put everything in a finally block.

Brian Gideon
  • 47,849
  • 13
  • 107
  • 150
  • 5
    Another way to look at it: Thread.Abort is a lot less useful than it used to be for the following reasons: deferred aborts while in unmanaged code, and finally blocks execute. If you are aborting the thread because the thread is doing unwanted work then both those things cause the thread to continue to do unwanted work. – Eric Lippert Aug 16 '11 at 19:01
  • 1
    @Eric: Yeah, interesting perspective. I hadn't considered *that* angle. – Brian Gideon Aug 16 '11 at 19:59
1

Generally speaking, Thread.Abort will kill threads, leaving the data they were processing at the time in an unknown state. The state being unknown, it's usually not safe to deal with that data anymore. However, when you're trying to terminate a process, you are not expecting to deal with that thread's data anymore, so why not abort it?

Gabe
  • 84,912
  • 12
  • 139
  • 238
  • Thread.Abort will trow an Exception and will (try to) execute all finally blocks. Not that much unknown state. – H H Aug 16 '11 at 16:12
1

Well, the problem with Thread.Abort() is that will abort the thread possibly in the middle of work. That might cause your state to be corrupted. That's why is advisable to use a volatile bool flag to control the thread, and let the thread finish its task gracefully, but based on that flag.

For more details, I recall this blog post.

Vladimir
  • 3,599
  • 18
  • 18