httpcontext object gives error when it is access in a different thread

I have a web form which has a button and when user clicks on the button then i am calling a aspx form (default2.aspx) using jquery.ajax method.

default2 create object of class1 and call dowork funtion in a different thread

now what is happening is i am trying to access htttpcontext in dowork function
but it gives me error on this  if (context.Cache["xxx"] == null)
it says object reference is not set.
and when i debug this i found out that context becomes null as soon as the ajax calls gets complete.

why it is happening, cannot i access server side cache in a different thread.
here is default2.aspx code
protected void Page_Load(object sender, EventArgs e)
        Class1 cls = new Class1();       
        ThreadStart s = new ThreadStart(cls.dowork);
        Thread d = new Thread(s);
        d.IsBackground = true;
        Response.Write("work doe");
here is dowork code
  public virtual void dowork()  {
        HttpContext context = HttpContext.Current;
        for (int i = 0; i < 5; i++) 
            if (context.Cache["xxx"] == null)
                context.Cache.Insert("xxx", "Current Loop Value:=" + i.ToString());
                context.Cache["xxx"] = "Current Loop Value:=" + i.ToString();
        context.Cache["xxx"] = "Done";

Open in new window

Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

abelConnect With a Mentor Commented:
You actually need a different approach, it seems. But rethinking your story, it seems that the working thread is not working too long. You can give it a pointer to the Session, which will be alive until the session times out, which is, since the last request, by default 30 minutes.

The good thing is, that the Session is automatically bound to the user/browser/client that made the request. So it won't become a problem if you would have multiple users simultaneously calling that method. Using Applciation or the Cache objects, you would have to add a lot of logic to prevent collision with other requests, since they all share the same memory space.

So, in short: use Sessions, that is your way to go.

-- Abel --
> why it is happening, cannot i access server side cache in a different thread.

that is quite correct. The HttpContext object is created only during the lifecycle of a request. You can access it, but it will only be set during a request and destroyed at the end of it. More precisely,it is part of HttpApplication class, which is created at the beginning of the request and destroyed at the end. With its destruction, its members, like Context, are destroyed also.

If you need information from the Context you should add that to your Worker class using different techniques. You cannot rely on it to keep a reference to the Context object. Trying to keep it alive (if at all possible) will probably yield unpredictable results with other requests.
ziorrinfotechAuthor Commented:
hi abel

is there any different way of storing a string which is globally accessible so that when i come back again even on a different page then i can access that string.
The new generation of project management tools

With’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

From your code above it seems that all you want to do is add some items to the Cache of the HttpContext. You may be aware of the fact that the HttpContext.Cache is basically the AppDomain's cache object, that you can retrieve by calling HttpRuntime.Cache. In turn, the HttpRuntime will callt he RuntimeConfig.GetAppConfig().Cache method to retrieve the cache (but it does a bit more too, so don't do this at home).

Because the HttpRuntime.Cache is a static property, it is a thread-safe operation to retrieve it.

-- Abel --
> that when i come back again even on a different page then i can access that string.

aha, that is about something totally different. Normally, you use Sessions for that. But you do not need a worker process in a separate thread for adding strings to a Session, do you? More globally is also possible (using Application[key] = value), but it is often preferred to use a database instead.

May I ask: why do you want to use a different thread? Is there a necessity for using a new thread or were you merely experimenting?
ziorrinfotechAuthor Commented:
here is my whole requirement.

I am working on a website which downloads around 20 to 30 xml files from ftp location and insert them into database.

and i want to show some sort of progress to the end user instead of just showing a gif image when this process is going.
this dowork function will in actual do all the above things. and I will write one more function will be called after regular interval to read the update string from the cache so that this string can be shown on the user browser.

so if I use Session or Application here would that be a better choice for me instead of cache?
and if yes then how can i access the session and application object inside a class code?
All Courses

From novice to tech pro — start learning today.