Thread aborting but still running, why?

  • Thread starter Thread starter Donald Adams
  • Start date Start date
D

Donald Adams

I need to be able to kill an instance of a class in my service should I
really need to. So I have the following code:

private void ServiceStart() {
_csManager=new Cana.Service.Manager();
_tService=new Thread(new ThreadStart(_csManager.Start));
_tService.Name="Manager";
_tService.Start();
}

private void ServiceStop() {
_tService.Abort();
_tService.Join();
_tService=null;
_csManager=null;
}

I create a thread to run my instance of this class so that I can kill it.
When I hit my stop button and step through each line in ServiceStop it
appears to work fine, however, when an event defined in _csManager fires it
keeps going. Doesn't setting my instances to =null destroy those instances,
why are they still running?

Thanks in Advance,
Donald
 
Donald,

I would guess that it's because setting objects to null (or Nothing for us
VB nuts) doesn't actually destroy them - it only makes them available for
garbage collection. As garbage collection does not happen immediately, your
instance of csManager could still be laying around in memory and processing
events as it needs to.

Look into the Dispose() and Finalize() methods. Before setting your object
to null, call Dispose() on it. In the dispose method, disconnect event
handlers and close any open resources (files, COM ports etc.). After calling
dispose, your object shouldn't be able to do any more processing until the
Garbage collector comes along.

It's only a guess, but I hope it helps,

Trev.
 
Back
Top