Claim resources/memory from thread single threaded apartment thread
-
09-09-2019 - |
Question
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??????
}
Solution
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).