Are thread-local variables retained after processing?

S

Steve - DND

If I declare a property as ThreadStatic, or I use the thread Context to set
values on, will they be retained after a page processes? When a page
finishes, is all data on the thread wiped before it is returned for use by
another page request?

Thanks,
Steve
 
S

Scott Allen

If I create a ThreadStatic variable on an ASP.NET thread servicing my
page request, than the ThreadStatic variable will still be around when
the thread finishes processing the request and returns to the free
thread pool.

The ASP.NET runtime will pick the thread out at a later point to
process another request, and the you'll find the ThreadStatic variable
is still around.

If you are looking for a container that will servce just the lifetime
of a single request, try the Items collection of the HttpContext class
(sample: http://odetocode.com/Articles/111.aspx). ASP.NET will ensure
the collection is properly initialized for each request.

I'd be cautious using ThreadStatic variables in ASP.NET. Since the
threads are managed by thread pool I would not have control over the
lifetime of the thread or my TheadStatic object.

HTH,
 
S

Steve - DND

Scott Allen said:
If I create a ThreadStatic variable on an ASP.NET thread servicing my
page request, than the ThreadStatic variable will still be around when
the thread finishes processing the request and returns to the free
thread pool.

The ASP.NET runtime will pick the thread out at a later point to
process another request, and the you'll find the ThreadStatic variable
is still around.

If you are looking for a container that will servce just the lifetime
of a single request, try the Items collection of the HttpContext class
(sample: http://odetocode.com/Articles/111.aspx). ASP.NET will ensure
the collection is properly initialized for each request.

I'd be cautious using ThreadStatic variables in ASP.NET. Since the
threads are managed by thread pool I would not have control over the
lifetime of the thread or my TheadStatic object.

That's kind of what I thought, as I didn't see different behavior when
playing with my own thread pools. The problem with using the HttpContext, is
that this needs to work with a business tier of code, which may be used on a
Windows machine as well. I guess I could try and work something where it
would first check for an HttpContext, and if not present, try to use the
thread's CurrentContext. Any other ideas?

Thanks,
Steve
 
S

Scott Allen

I think I would abstract away the source of information into another
class/object which could check for an HttpContext. It follows the
'keep it simple' rule and keeps all the Http specific "stuff" in one
place.

--s
 
R

Rick Spiewak

If you want data associated with a page, why not use ViewState? Or, if it's
for the session, Session state?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top