views:

47

answers:

2

I want to store per-thread data in an ADO.NET Data Service. Is it safe to use the ThreadStatic attribute on my thread-specific static variable, or will I run into problems? My concern is that my ThreadStatic variable(s) won't be garbage collected after the request is completed and the thread dies.

If there's a better way to do what I'm trying to do, please let me know. This just seems like the simplest solution.

Any information would be very helpful, thanks!

A: 

Any variables which are tagged with ThreadStatic will have the lifetime at least as long as the thread on which they are created. After that Thread terminates they are subject to garbage collection like any other value.

JaredPar
+2  A: 

I've found that the DataService object that's created for each request is on a thread marked with IsThreadPoolThread = true, so using the [ThreadStatic] attribute is not appropriate in this case, as information from previous requests could be available by later ones (not desirable).

Pwninstein