Groups | Blog | Home
all groups > asp.net caching > may 2004 >

asp.net caching : Accessing HttpContext.Current.Cache from new Thread


Ken Everett
5/4/2004 7:26:03 AM
I have a web page that executes a potentially lengthy method declared on an object in a referenced assembly

Nested within this method is a call to a static method which retrieves values from cache via the HttpContext.Current.Cache object

In order to make the web application more responsive, I am now trying to execute this long running method in a new thread

My problem: When the process running on the new thread reaches the reference to the HttpContext.Current.Cache object in the static method I get a runtime exception saying "Object reference not set to an instance of an object."

Alvin Bruney [MVP]
5/4/2004 10:59:50 AM
[quoted text, click to view]

You will need to pass a reference to the httpcontext to the thread. That's
the only way this will work because the thread is not allowed to manipulate
the context object since it belongs to the main thread.

[quoted text, click to view]

yup, something like that. The way you establish context is by passing in a
reference to child thread from the main thread. You can do this in your
thread class constructor

public class thread
{
public start(HttpConstext current)....

the call would be something like thread t = new thread(HttpContext.Current)

--
Regards,
Alvin Bruney
[ASP.NET MVP http://mvp.support.microsoft.com/default.aspx]
Got tidbits? Get it here... http://tinyurl.com/27cok
[quoted text, click to view]

Ken Everett
5/4/2004 11:00:11 AM
Thanks for the reply. However, I'm not sure I'm clear on the details of
your suggestion.

Are you implying that I should create my own thread class that inherits
from System.Threading.Thread and override the Start method?

Or do I try something like this:

MyClass myObject = new MyClass();

Thread myThread = new Thread( new ThreadStart( myObject.LongMethod(
HttpContext ) ));





*** Sent via Developersdex http://www.developersdex.com ***
John Saunders
5/4/2004 12:51:08 PM
Alvin is correct, of course, but let me add something just in case: if your
separate thread intends to interact with the original page, it will have a
problem, as the page will be gone by the time the thread finishes.

In fact, although it makes sense that accessing HttpContext.Current.Cache
will work after the request is complete, I'd be careful about using any
other part of the HttpContext object from a separate thread. That's the
context of the request, and if the request is over, you have to wonder
what's in the context...
--
John Saunders
John.Saunders at SurfControl.com


[quoted text, click to view]

Alvin Bruney [MVP]
5/4/2004 1:25:27 PM
[quoted text, click to view]
Yes.

More important than what I said is John's reply. The page is gone by the
time you make the cache access. You will need to do a join on the main
thread to keep the page waiting for the thread to accomplish its mission
otherwise when the child thread is ready with its results, the boat would
have sailed away long ago. If you can't cause the main thread to wait for
the child thread to finish, it makes no sense to thread basically.

--
Regards,
Alvin Bruney
[ASP.NET MVP http://mvp.support.microsoft.com/default.aspx]
Got tidbits? Get it here... http://tinyurl.com/27cok
[quoted text, click to view]

Ramesh Nayanala
5/7/2004 3:36:02 PM
ThreadStart delegate wraps a method with NO parameters
So you might need to save the reference to HttpContext.Current.Cache to a global variable
and access it from the new thread.
Did I mention synchronization ?

Thank
John Saunders
5/7/2004 8:11:54 PM
[quoted text, click to view]

public class MyClass
{
private HttpContext _ctx;
public MyClass(HttpContext ctx){_ctx = ctx;}

public void Start()
{
ThreadStart start = new ThreadStart(MainThreadRoutine);
Thread.Start(start);
}

private void MainThreadRoutine()
{
// Do something lengthy using the HttpContext in _ctx
// ...
// But by the time you get here, it's all over, and _ctx is
meaningless, because the request is OVER
// and you missed the boat!
}
}

// In your page class:
private void Page_Load(object sender, EventArgs e)
{
MyClass myClass = new MyClass(HttpContext.Current);
myClass.Start();
}

What you're trying to accomplish cannot be accomplished easily in ASP.NET
1.0 or 1.1. According to the latest issue of MSDN Magazine, MS appear to
have a partial solution for this problem in ASP.NET 2.0. In the meantime, I
recommend that you finish every other part of your application before
returning to this "multiple threads in ASP.NET" issue. When everything else
is done, you can spend some time reinventing the wheel.
--
John Saunders
John.Saunders at SurfControl.com

AddThis Social Bookmark Button