When is a Thread considered dead?

  • Thread starter Thread starter cider123
  • Start date Start date
C

cider123

I have a simple Window Service that loads up a list of Folders to
monitor, along with parameters for a maintenance profile.

Every time the interval is reached, the Window Service spawns off a
Worker thread:

....
_ActiveThread = new Thread(new ThreadStart(_WorkerThread.ThreadStart));
_ActiveThread.Start();

The method for "ThreadStart" simply loads a list of files in a defined
folder, and deletes anything older than ## days.

Some of these Folders are remote sites (various relays across the U.S.
at different company locations) so the work time required to process
thousands of files in each Folder could be fast or slow depending on
bandwidth at the time of execution--hence the reason I spawn separate
threads off.

I created an ArrayList and stored the "_ActiveThread" created, and in
another routine I have on a Timer I check the "ThreadState".

When the task is finished, the ThreadState is in a "Stopped" state and
the "IsAlive" is False.

That's great I guess, but what does this mean to the system? Are
Threads in a "Stopped" state going to eventually get Garbage Collected
when the system is ready? If not, how do I gracefully terminate it? I
have a "Dispose" Interface, but that doesn't necessarily terminate the
Thread. From what I've read, Dispose is just another method that you
can call off the object even though it's an Implemented method.

I have about 10 Threads that spawn off simultaneously at the moment.

Should one cycle through the ArrayList, if in a "Stopped" state call
the "Abort()" then remove the reference and let the system eventually
GC it? Or how exactly does that work?

I'm still searching newsgroups for answers, but if anyone has anything
to offer on comments.. I'm always game to learn new things.
 
Maybe I'm wrong, but I don't think you need to dispose a thread.
You can check how many thread your process is using by looking at the
Task Manager, add a thread Count column from View.
 
Probably not, but I was just trying different things.

I did add the column for "Threads" in the Task Manager and everytime
the interval cycle fires off the execution of Threads, it added more.
As if it never released them (at least, right away).

This very well may be because I was storing a reference to the Thread
object in the ArrayList though, but once a minute I would .Clear() the
Arraylist and reload Folder references. I log info to a Database so I
know the jobs were completed way before it cycles again.

I'm just looking at educating myself on the best way to handle and deal
with the execution and termination of Threading in a multi-threaded
environment.
 
I don't think that the way you are doing it now is a good thing. If
anything, you should have the objects that perform the work take some sort
of reference to a callback (through an interface or through a delegate).
When the thread finishes its work, call the callback, and notify that the
work is complete.

You don't have to worry about the thread, it will be handled by the
system eventually (there should be an implementation of IDisposable on the
Thread object, IMO).

I think a better approach to this overall would be to use the
ThreadPool, perhaps. It is a pool of threads that is used to handle short
tasks (such as deleting a file).

What you could do is load an array list or some sort of collection up
with the names of the files to delete. Then, fire off 10 work items. Each
of these items would delete a file. When complete, the work item would
queue up another work item with the name of the next file to be deleted. If
there are no more items in the collection, then do not fire up another work
item.

Hope this helps.
 
Back
Top