Geeks With Blogs
Greg Young Greg.ToString()

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

 

 

Posted on Thursday, January 5, 2006 10:18 AM | Back to top


Comments on this post: Thread.Abort

# ReverseMortgage.info-junction.net : What is reverse mortgage.
Requesting 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
Left by BobSmith Charles on Feb 11, 2009 10:17 AM

# Free membership for internet largest dating service
Requesting 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>
Left by usedgexutouro on Feb 18, 2009 10:07 AM

# Fund von wertvollem Diamantring
Requesting 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
Left by KaleescalkSag on Nov 21, 2009 10:42 PM

# re: Thread.Abort
Requesting Gravatar...
Great! Really usefull...cheers!
Left by Jeena on Feb 23, 2011 10:44 AM

Your comment:
 (will show your gravatar)


Copyright © Greg Young | Powered by: GeeksWithBlogs.net