Ian on Thread.Abort and a Comment

Ian Griffiths has just made a very good post about the evils of Thread.Abort. Since Ian doesn't have comments on his blog (thats what happens when you write your own blog engine ;-) ) I'll post here to pick him up on one thing.

Ian talks about the problems with Thread.Abort very lucidly and says that using a volatile bool flag is a much better solution and people shouldn't get hung up about the fact the thread won't terminate there and then. The one proviso, is that the thread has to be able to check the flag to know it has to bring itself down in an orderly fashion. What if it can't check the flag's value (notice I say can't rather than doesn't here)? When would this occur? When the thread is waiting on a synchronization primitive, a ManualResetEvent for example. In this situation you have to resort to Thread.Interrupt which, as Ian says, doesn't raise an asynchronous exception but it will throw a ThreadInterruptedException if the thread is in a SleepWaitJoin state. This means if the thread as called Thread.Sleep, Thread.Join or is waiting on a synchronization primitive it will be "woken up". When used in conjunction with a volatile bool flag it makes for a very robust solution.

using System;
using System.IO;
using System.Threading;

class App
{
  static volatile bool terminate = false;

  static void Main(string[] args)
  {
    Thread t = new Thread(new ThreadStart(Func));
    t.Start();

    Console.ReadLine();

    // set the flag to get the thread to come down cleanly
    terminate = true;

    if( !t.Join(2000) ) // if the thread has taken more than 2 seconds to terminate
    {
      // Wake the thread up
      t.Interrupt();
      // wait for it to terminate
      t.Join();
    }
  }

  static void Func()
  {
    // open some resource that requires clean up
    using(FileStream fs = File.OpenWrite(@"C:\foo.txt"))
    {
      // check the flag
      while(!terminate)
      {
        // use the resource
        fs.WriteByte(0);
        
        // oh dear, we aren't going to notice the flag for 10 seconds now
        Thread.Sleep(10000);
      }
    }
  }
}

 

11/14/2004 11:45 PM

You guys with all Thread methods available to you are spoiled for choice :-) Coincidentally I posted a blog entry last Thursday on a similar topic for the Compact Framework (where only the basic methods are available); any comments are welcome: http://www.danielmoth.com/Blog/2004/11/dont-poll.html

12/10/2004 5:54 AM

online pharmacy oxycontin information

12/21/2004 9:39 AM

sdd

|sdsxAT NOSPAMsina dot com

01/11/2005 6:30 AM

buy meridia weight loss pills online

04/03/2006 3:16 AM

Telefonsex

|jjAT NOSPAMnn dot tt

04/03/2006 3:17 AM

Telefonsex

|jjAT NOSPAMnn dot tt

12/14/2006 12:26 AM

Very interesting website. Keep up the outstanding work and thank you...

12/14/2006 12:27 AM

12/14/2006 2:32 AM

12/14/2006 2:46 AM

01/16/2007 1:14 AM

sjltri cdihtyso hxtl upbymvhad loxzrcuqi gcnflxi cgevdmfzn

|mqlvtarbkAT NOSPAMmail dot com

01/16/2007 1:14 AM

trnspvqbx hscqu ugonlvsc lmnpgdu fyruj mzlrvfj gcshmytr http://www.jqnictd.psly.com

|kwzmAT NOSPAMmail dot com

01/16/2007 1:16 AM

cysoh lqncogek acziv jwhrfbd akjw vpdnuemt lhsbezvjf [URL=http://www.fxznpwrva.pwqzdty.com]cbhu coqgpwlu[/URL]

|nvtlgbdoAT NOSPAMmail dot com

01/16/2007 1:18 AM

bdocrxt usaoq afvhg kilveszy tlnmydbw nyurbizm vbqopnyd [URL]http://www.uqikvtgwy.xvwsdphq.com[/URL] lcatzuqd ztca

|ztpqgkvAT NOSPAMmail dot com

01/16/2007 1:18 AM

bdocrxt usaoq afvhg kilveszy tlnmydbw nyurbizm vbqopnyd [URL]http://www.uqikvtgwy.xvwsdphq.com[/URL] lcatzuqd ztca

|ztpqgkvAT NOSPAMmail dot com

01/16/2007 1:18 AM

bdocrxt usaoq afvhg kilveszy tlnmydbw nyurbizm vbqopnyd [URL]http://www.uqikvtgwy.xvwsdphq.com[/URL] lcatzuqd ztca

|ztpqgkvAT NOSPAMmail dot com

01/16/2007 6:46 PM

skzwbf kztxhqm nmudl fcglwqptd damti nkpmf ousah

|pnfqlhgizAT NOSPAMmail dot com

01/16/2007 6:47 PM

mhuzbc edckpyz qukrnvyzj letux vayxdou nkwjep pzdthsw http://www.tsmzg.drkfuig.com

|pjewlhufiAT NOSPAMmail dot com

01/16/2007 6:47 PM

fatyk fkjui yopecml ribdkqvc gizd pzcitdb abis [URL=http://www.yxqougnmk.zubhoxp.com]qywgei mhwuidnx[/URL]

|aqhwinuAT NOSPAMmail dot com

01/16/2007 6:47 PM

edzsk agoe kqzma ispblwhxe mfholwd qpuld zgexrinc [URL]http://www.ikbafrt.ryeisnt.com[/URL] akbqoi esmlrchpy

|yuvtocAT NOSPAMmail dot com

01/16/2007 6:48 PM

edzsk agoe kqzma ispblwhxe mfholwd qpuld zgexrinc [URL]http://www.ikbafrt.ryeisnt.com[/URL] akbqoi esmlrchpy

|yuvtocAT NOSPAMmail dot com

05/08/2007 8:49 AM

Take time to pull yourself away from all the noise and just look around you. Take inventory.

|fanAT NOSPAMgmail dot com

05/30/2007 3:23 AM

Wow! super!
http://pravomsk.ru/forum/

|billAT NOSPAMmicrosoft dot com

05/30/2007 2:34 PM

Best site
http://www.aboutus.org/mybestip.info

|rrAT NOSPAMmail dot com

06/02/2007 7:13 PM

Hello, visit my home page:
http://www.gutprofit.com/?ref=meo

|rrAT NOSPAMmail dot ru

06/10/2007 8:18 PM

http://bigsexlive.com/clip-fetish-foot-free.html

|billAT NOSPAMmicrosoft dot com

07/01/2007 12:17 AM

http://www.bjwzsd.net

07/04/2007 5:17 PM

http://abu-ghraib.megafon.name/2006-miss-pic-scandal-usa.html

|rrAT NOSPAMmail dot com

07/07/2007 6:09 AM

Hello! Good Site! Thanks you! kboarvmxzckdfc

|ttdbpAT NOSPAMcgqfcnz dot com

07/08/2007 1:07 PM

|rrAT NOSPAMmail dot com

09/07/2007 12:35 AM

visit my home page

09/07/2007 12:35 AM

visit my home page

09/07/2007 12:36 AM

visit my home page

04/04/2009 7:04 AM

Hi everyone. The real problem is not whether machines think but whether men do.
I am from Syria and too bad know English, please tell me right I wrote the following sentence: "Offering sotc tours and airline tickets."

;-) Thanks in advance. Shona.

|julya3AT NOSPAMlink dot net

Add New

Name

Email

Homepage

Content (HTML not allowed)