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."
Educated guess: I'm thinking I need to establish the context of the new thread to be the same as that of the web page which spawns the thread. Am I headed in the right general direction? Can anyone offer any specific example or explanation of how to implement multi-threading with access to the web application cache?
>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."
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.
>Educated guess: I'm thinking I need to establish the context of the new
> >thread to be the same as that of the web page which spawns the thread.
> >Am I headed in the right general direction? Can anyone offer any specific
> >example or explanation of how to implement multi-threading with access >to
>the web application cache?
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)

Signature
Regards,
Alvin Bruney
[ASP.NET MVP http://mvp.support.microsoft.com/default.aspx]
Got tidbits? Get it here... http://tinyurl.com/27cok
>I have a web page that executes a potentially lengthy method declared on an
>object in a referenced assembly.
[quoted text clipped - 15 lines]
> example or explanation of how to implement multi-threading with access to
> the web application cache?
John Saunders - 04 May 2004 17:51 GMT
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...

Signature
John Saunders
John.Saunders at SurfControl.com
> >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
[quoted text clipped - 40 lines]
> > example or explanation of how to implement multi-threading with access to
> > the web application cache?
Ken Everett - 04 May 2004 19:00 GMT
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 ) ));
Alvin Bruney [MVP] - 04 May 2004 19:25 GMT
> Thread myThread = new Thread( new ThreadStart( myObject.LongMethod(
> HttpContext ) ));
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.

Signature
Regards,
Alvin Bruney
[ASP.NET MVP http://mvp.support.microsoft.com/default.aspx]
Got tidbits? Get it here... http://tinyurl.com/27cok
> Thanks for the reply. However, I'm not sure I'm clear on the details of
> your suggestion.
[quoted text clipped - 8 lines]
> Thread myThread = new Thread( new ThreadStart( myObject.LongMethod(
> HttpContext ) ));
Ramesh Nayanala - 07 May 2004 23:36 GMT
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
Ramesh
John Saunders - 08 May 2004 01:11 GMT
> 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 ?
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.

Signature
John Saunders
John.Saunders at SurfControl.com