Blog Stats
  • Posts - 99
  • Articles - 5
  • Comments - 96
  • Trackbacks - 106

 

Thread.Abort

I noticed that the last post I made did not aggregate, most likely because I posted it then deleted it then reposted it.

 

Thread.Abort is evil. Especially in the case of dealing with unmanaged resources as chances are they will never be released.

 

A common pattern for dealing with unmanaged resources is to make a managed wrapper. This way if the resource is forgotten about, the object can release the resource when it is garbage collected. Thread.Abort offers some intresting problems with this as you may get aborted between the time you get the resource and the time that you put the wrapper around your unmanaged object to protect it.

Instead of using thread.abort, one should always be using a volatile “stop“ variable. The thread should be checking this variable at intervals to see if its time to stop. If you need to synchronously stop a thread, set the variable then call Join(timeout). If you make it through your timeout and could possibly be dealing with a dead thread, then and only then should you consider using thread.abort.

Here is a basic class illustrating, this is not production code, just typed it into notepad real quick so treat it as such.

 /// <summary>
 /// Base class to inherit long running threads from
 /// </summary>
 public class ThreadedTaskBase
 {
  private Thread m_Thread;
  private volatile bool m_Terminate;
  protected bool Terminate {
   get { return m_Terminate; }
  }

  protected abstract void Run();

  /// <summary>
  /// Starts the thread
  /// </summary>
  public void Start() {
   lock(this) {
    if(m_Thread == null) {
     ThreadStart ts = new ThreadStart(this.Run);
     m_Thread = new Thread(ts);
     m_Thread.Start();
    } else {
     throw new Exception("Thread already created");
    }
   }
  }

  /// <summary>
  /// Stops the thread
  /// </summary>
  public void Stop() {
   lock(this) {
    m_Stop = true;
    m_Thread = null;
   }
  }

  public void StopSynchronous() {
   lock(this) {
    if(m_Thread != null) {
     m_Stop = true;
     bool stopped = m_Thread.Join(10000); //wait 10 seconds
     if(!stopped) { m_Thread.Abort(); }
     m_Thread = null;
    } else {
     throw new Exception("thread already stopped");
    }
   }
  }

  public ThreadedTaskBase()
  {
  }
 }

 

 

In general one would probably not waznt too use a base class for this but instead a wrapper class for a delegate ... such that any delegate can be run in this manner, to change the above code to be a wrapper, add the delegate method to the constructor and you should be off to the races.

Greg

 

 


Feedback

# ReverseMortgage.info-junction.net : What is reverse mortgage.

Gravatar As the financial crisis grips the entire world. We need to be more informed
of the options and choices that we have. Found this website that has a lot
of information regarding reverse mortgage and other mortgage related issues.

The website is dedicated to Mortgage problems faced by real people and the
answers to it. You will find a lot of questions and the answers to those
questions posted by reall real people so it is very informative.

So see the answers to you mortgage questions at What Is Reverse Mortgage 2/11/2009 10:17 AM | BobSmith Charles

# Free membership for internet largest dating service

Gravatar Found this on another forum. Thought it might be use full. The website gives exact steps to get a free
gold membership for the worlds largest dating service website.

Largest Dating Website

Warning Adult Content On Website
Enjoy <IMG>http://www.sharemyphotos.info/e/K.gif</IMG> 2/18/2009 10:07 AM | usedgexutouro

# Interesting Information

Gravatar I found lots of interesting information on geekswithblogs.net. The post was professionally written and I feel like the author has extensive knowledge in the subject. geekswithblogs.net keep it that way. 5/1/2009 12:23 AM | Online Payday Loans

#  Fund von wertvollem Diamantring

Gravatar
Nelson J. zeigt einige der vielen Münzen, Schmuckstücke und jahrhundertalte Artefakte, die er mit dem Unterwasser Detektor gefunden hat.<img>http://c3.ac-images.myspacecdn.com/images02/106/l_7eb2926624ff418f8f545aa31e13cd6a.jpg</img>metalldetektor garrett garret 11/21/2009 10:42 PM | KaleescalkSag

Post a comment





 

 

 

Copyright © Greg Young