Last post Aug 09, 2013 05:38 AM by HeartattacK
Aug 08, 2013 04:33 PM|halcyon1234|LINK
I have some utilities that cache data in HttpContext.Current.Items
I tried running a program in a thread that used these utilities. Needless to say they crashed since HttpContext.Current is null inside the thread.
Is there an equiv. collection I can use-- a hashtable that lasts for the lifetime of the thread, and is confined only to that thread?
Aug 09, 2013 05:38 AM|HeartattacK|LINK
You can have a static Dictionary field to which you apply the [ThreadStatic] attribute. That'll ensure one per thread. DO NOT use an inline initializer [i.e. private satic Dictionary<T,T2> dict = new Dictionary<T, T2>()] as that will only initialize it once.
If the per thread dictionary is going to be used from other threads, consider using a ConcurrentDictionary instead.