Question

if i put things in c# TLS when does it get cleaned, or is it my job to clean it?

I mean

a) will I always start with an empty TLS no matter how a thread starts a given piece of work (a QueueUserWorkItem thread , WCF invoked,...)

b) If I put expensive things in TLS will they be GC'd when the thread completes its work. Note I dont say 'ends' since I assume most .net threads come from one or more pools and so they don't necessarily end as far as the OS is concerned. Or is it my job to always 'finally' myself to make sure I dont leave a mess behind me

Was it helpful?

Solution

TLS entries will survive a thread being placed back in the pool.

So, in other words, it's your responsibility to clean everything you put in the TLS if you are going to use threads from the System.Threading.ThreadPool class.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top