Revision of Thread.Abort () - is it Safe?

MSDN for porting legacy multithreaded applications (from this thread exception page ):

In the general case, this change will lead to the identification of previously unrecognized programming problems so that they can be fixed. In some cases, however, programmers could take advantage of back-up support, for example, to stop threads. Depending on the situation, they should consider one of the following migration strategies:

Restructure the code so that the stream exits gracefully when receiving a signal.

Use the Thread.Abort method to interrupt the thread.

If a thread must be stopped to complete a process, make the thread a background thread so that it automatically terminates when the process terminates.

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

This suggests that use Thread.Abortis an appropriate way to terminate a stream. Did something change while I was watching? The last thing I heard is that it can lead to unexpected behavior, so it should not be used.

+5
source share
3 answers

Thread.Abort much safer than before, for the following reasons.

  • The runtime will postpone the interrupt while execution is in unmanaged code.
  • Cancel will allow the execution of finallyblocks.

However, there is still a problem with when introduced ThreadAbortException. Consider this code.

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

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

32- , value , Thread.Abort, ThreadAbortException value. DateTime - 8 , .

, finally , , , . finally.

+3

, Thread.Abort , , . , . , , , ?

+1

, Thread.Abort() , , , . . volatile bool flag , .

.

+1

All Articles