Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Please <em>don't</em> use <code>Thread.Abort</code> as recommended by the other answers so far, unless you want your program to be in an unknown state (see articles by <a href="http://www.interact-sw.co.uk/iangblog/2004/11/12/cancellation" rel="nofollow noreferrer">Ian Griffiths</a> and <a href="http://www.ondotnet.com/pub/a/dotnet/2003/02/18/threadabort.html" rel="nofollow noreferrer">Chris Sells</a> for more info). If closing the form should actually be killing the app, you're <em>probably</em> okay - but in that case I'd recommend just using background threads anyway, which will automatically die when all foreground threads have terminated.</p> <p>From Joe Duffy's <a href="http://rads.stackoverflow.com/amzn/click/032143482X" rel="nofollow noreferrer">"Concurrent Programming in Windows"</a>:</p> <blockquote> <p>There are two situations in which thread aborts are always safe:</p> <ul> <li>The main purpose of thread aborts is to tear down threads during CLR AppDomain unloads. [...]</li> <li>Synchronous thread aborts are safe, provided that callers expect an exception to be thrown from the method. [...]</li> </ul> <p>All other uses of thread aborts are questionable at best. [...] While thread aborts are theoretically safer than other thread termination mechanisms, they can still occur at inopportune times, leading to instability and corruption if used without care.</p> </blockquote> <p>(Synchronous thread aborts are when the thread aborts itself, rather than being aborted by another thread.)</p> <p>For <em>graceful</em> shutdown (without risking getting into odd states) use a flag which is set periodically from the form and checked from the other threads - taking the memory model into account (e.g. either making the flag volatile or using a lock each time you test or set it). See my <a href="http://pobox.com/~skeet/csharp/threads/shutdown.shtml" rel="nofollow noreferrer">article on the topic</a> for an example.</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