Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8470129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T16:25:50+00:00 2026-06-10T16:25:50+00:00

I read from < Essential ASP.NET with Examples in C# > the following statement:

  • 0

I read from < Essential ASP.NET with Examples in C# > the following statement:

Another useful property to know about is the static Current property
of the HttpContext class. This property always points to the current
instance of the HttpContext class for the request being serviced. This
can be convenient if you are writing helper classes that will be used
from pages or other pipeline classes and may need to access the
context for whatever reason. By using the static Current property to
retrieve the context, you can avoid passing a reference to it to
helper classes. For example, the class shown in Listing 4-1 uses the
Current property of the context to access the QueryString and print
something to the current response buffer. Note that for this static
property to be correctly initialized, the caller must be executing on
the original request thread, so if you have spawned additional threads
to perform work during a request, you must take care to provide access
to the context class yourself.

I am wondering about the root cause of the bold part, and one thing leads to another, here is my thoughts:

We know that a process can have multiple threads. Each of these threads have their own stacks, respectively. These threads also have access to a shared memory area, the heap.

The stack then, as I understand it, is kind of where all the context for that thread is stored. For a thread to access something in the heap it must use a pointer, and the pointer is stored on its stack.

So when we make some cross-thread calls, we must make sure that all the necessary context info is passed from the caller thread’s stack to the callee thread’s stack.

But I am not quite sure if I made any mistake.

Any comments will be deeply appreciated.

Thanks.

ADD

Here the stack is limited to user stack.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T16:25:51+00:00Added an answer on June 10, 2026 at 4:25 pm

    There are four things working together to cause the behavior you are asking about:

    1. HttpContext is an instance object whose reference can be found in HttpContext.Current
    2. Thread is also an instance object whose reference can be found in Thread.CurrentThread
    3. Thread.CurrentThread is static but references a different Thread object in every thread
    4. HttpContext.Current actually points to Thread.CurrentThread.ExecutionContext.IllogicalCallContext.HostContext

    Conclusions we can draw from the above givens:

    1. Because HttpContext is an instance object and not static we need its reference to access it
    2. Because HttpContext.Current actually points to a property on Thread.CurrentThread, changing Thread.CurrentThread to a different object will likely change HttpContext.Current
    3. Because Thread.CurrentThread‘ changes when switching threads, HttpContext.Current also changes when switching threads (in this case HttpContext.Current becomes null).

    Bringing this all together, what causes HttpContext.Current to not work in a new Thread? The Thread.CurrentThread reference change, which happens when switching threads, changes the HttpContext.Current reference, which prevents us from getting to the HttpContext instance we want.

    To reiterate, the only magic thing going on here is Thread.CurrentThread referencing a different object in every Thread. HttpContext works just like any other instance object. Since threads in the same AppDomain can reference the same objects, all we have to do is pass a reference for HttpContext to our new thread. There is no context info to load or anything like that. (there are some fairly serious potential gotchas with passing around HttpContext to other threads but nothing to prevent you from doing it).

    A few final side notes I came across while researching:

    1. In some cases a Thread’s ExecutionContext is ‘flowed’ (copied) from one Thread to another. Why then is HttpContext not ‘flowed’ to our new Thread? Because HttpContext doesn’t implement the ILogicalThreadAffinative interface. A class stored in the ExecutionContext is only flowed if it implements ILogicalThreadAffinative.

    2. How does ASP.NET move HttpContext from Thread to Thread (Thread-Agility) if it isn’t flowed? I’m not entirely sure, but it looks like it might pass it in HttpApplication.OnThreadEnter().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string read from another source such as \b\bfoo\bx. In this case,
We're using the following pattern to handle caching of universal objects for our asp.net
I read from Outlook Contacts my Contacts and then I add the Contact to
I read from some books that Phusion Passenger is the answer to easy Ruby
From what I've read from Herb Sutter and others you would think that volatile
I am trying to read from a file and encrypt the data using AES
How can I read from and write to my Galaxy Nexus phone, using MTP
How would I read from an Excel sheet and load the marked selection (Area)
History: I read from one of Knuth's algorithm book that first computers used the
I want to read from stdin five numbers entered as follows: 3, 4, 5,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.