Life of a thread

  • Thread starter Thread starter Zeng
  • Start date Start date
Z

Zeng

Hi,
If a thread goes out of scope and the working proc (ThreadStart) is done
processing, does it eventually get finalized just like unreferenced memory?

thanks!
 
thread with in method Foo, and this thread is referenced by a local
variable... will the thread be finalized after it has completed it’s work and
the local reference has gone out of scope?

Then yes, once you start a thread, it can run independently of it’s
reference, but when both are gone (ie no remaining reference), then the
thread will be eligible to be GCed and you can expect to have that happen.
 
Zeng said:
Hi,
If a thread goes out of scope and the working proc (ThreadStart) is done
processing, does it eventually get finalized just like unreferenced
memory?

thanks!


Yes. Unless, of course, you have unmanaged resources (like a database
connection) in the thread. Then you may eventually wind up with a
noticeable leak. You can use a class to govern the thread. Make sure it
inherits : IDisposable and add a ~Destructor to handle all unmanaged
resources.

You may also want to run the worker thread with the IsBackground property
set to true so destruction happens immediately if the main app is
terminated.

J. Buelna - Houston, TX
 
Back
Top