Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>With a contrived test, you can prove anything.</p> <p>All you have proved is that with <em>the code you wrote for your test</em>, <code>Thread.Abort</code> seems to work fine.</p> <p>The problem is, however, that as soon as you start using things that needs to be disposed of, all hope is lost.</p> <p>For instance, try this code:</p> <pre><code>using (Stream stream = new FileStream(@"C:\Test.txt", FileMode.Create, FileAccess.ReadWrite, FileShare.None)) { Thread.Sleep( Random.Next( 45, 55 ) ); } </code></pre> <p>Now, run this for a while and tell me if that still works.</p> <p>The problem appears when the code has left your sleep-call, and is inside the implicit finally-block for the using-block, and is just about to close your stream, and then you abort it.</p> <p>The problem with <code>Thread.Abort</code> is that it can happen <em>anywhere</em>, even within code that isn't supposed to throw exceptions.</p> <p>For instance, do you really expect the following code to crash <em>after</em> the if-expression has been evaluated, but <em>before</em> the <code>Dispose</code>-call has gone through?</p> <pre><code>if (_ObjectToDispose != null) { _ObjectToDispose.Dispose(); _ObjectToDispose = null; } </code></pre> <p>What if it happens right after the call to <code>.Dispose</code>? The field will still have a non-null value, which could lead to subtle problems elsewhere.</p> <p>What if you do this:</p> <pre><code>IDisposable objectToDispose = Interlocked.Exchange(ref _ObjectToDispose, null); if (objectToDispose != null) objectToDispose.Dispose(); </code></pre> <p>With this code, you grab the value, replaces it with null, and then before you get around to calling Dispose, your <code>ThreadAbortException</code> happens, which will just leave the object.</p> <p>Let me just drive the point home:</p> <blockquote> <p><strong>Thread.Abort should <em>never</em> be used, except in scenarios where you need to terminate the program (or tear down a custom AppDomain with threads running in it). You should <em>never</em> call Thread.Abort and then continue running.</strong></p> </blockquote> <p>Unless you need to plan bugs into your future schedule. In that case, go right ahead and use <code>Thread.Abort</code>, because I can almost guarantee you will have problems.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload