views:

122

answers:

1

I am using following single threaded appartment. I am unable to reclaim memory/other resources from thread object. Actullay I want to wrap my thread in try catch and fianlly block. try and catch are done. But I am unsure about finally block. What code, property or function do I need to call in finally block.

System.Threading.Thread myThread = null;
    try 
    {
        myThread = new System.Threading.Thread(functionAddressETC)
        myThread .SetApartmentState(System.Threading.ApartmentState.STA);
        myThread .Start();
        myThread .Join();
    }
catch(Exception ex)
{}
finally
{
   // I need help in finally block. I need to reclaim all my resources
   //what function do i need to call here??????
}
+1  A: 

The GC will reclaim memory, finalizers will free native resources. The only way to ensure these processes happen is to ensure that objects you no longer need are unreferenced.

A thread does not have any memory/resources of its own apart from its stack, which will be cleaned up by the Join shutting down the thread (unless you have something messing with duplicating native thread handles: in which case when the last handle is closed).

Richard
you mean i need to check following code in finally block.finally{ if (myThread != null) //release code //But what function do i need to call here????}
Syed Tayyab Ali
You don't have to do anything to cleanup the Thread itself. You may need to cleanup any unmanaged resources allocated in the thread proc.
Arnshea
@Syed: The Join is all you need.
Richard
Thank you Richard and Arnshea.
Syed Tayyab Ali