HTTPContext in Threading

G

Guest

Hi All
I have problem in using Threading.
I have ASP.NET application in which I am using multithreading to start a
process.
Now the object instances which are used in this thread access
HttpContext.Current to get Session Variable value. Here my code fails. Thread
on which the process is running does not have HTTPContext.Current object.
How can I pass-on HTTPContext.Current info to thread.

I used WindowsImpersonationContext for impersonation, like wise is there
anything which i can do for HTTPContext

Thanks in advance,
Wish you all a very HAPPY NEW YEAR - 2005
Makarand
 
D

David Jessee

I'm not sure that you'd want to do that anyway.

abstractly, it is possible for a user request to end before your new thread
has terminated. once that has happened, that initial Context does not exist
anymore. This may work, though......I'll do it in VB

<Serializable()>Public Class UserSesion
Private _someValue as Int32
Public Property SomeValue as Int32
Get
return _someValue
End Get
Set (value as Int32)
_someValue = value
End Set
End Property

Private Sub New()
'manages lifecycle
End Sub

Public Shared Readonly Property Current() as UserSession
if Httpcontext.Current.session("UserSession") is nothing then
Httpcontext.Current.session("UserSession") =new UserSession
return ctype(Httpcontext.Current.session("UserSession")
,UserSession)
End Property
End Class

Use a class structured like this for your session management in
ASP.NET....then when you need to spin your new thread off, pass it
UserSession.Current. That way you can give your Session State to the new
thread.
 
B

Bruce Johnson

The problem you have is that HttpContext is basically a thread-level
value. It's actually stored in the CallContext, but for the purposes of
this discussion, assume that it's only active on the current thread.

If you attempt to pass the current context into a thread, it will
continue to have a value only until the main thread terminates. Which
will tend to give rise to the kind of thread-related bugs that are a joy
to work on.

The best way to pass the session values that you're looking for into a
separate thread is to extract them from a the Session object into
strings of their own and pass those string variables into the thread.
But keep in mind that you will not be able to update the Session values,
nor will you be able to use Response.Write to get information back to
the client.

Hope that helps.

Bruce Johnson [.NET MVP]
http://www.objectsharp.com/blogs/bruce
 
J

John Saunders

Makarand Keer said:
Hi All
I have problem in using Threading.
I have ASP.NET application in which I am using multithreading to start a
process.
Now the object instances which are used in this thread access
HttpContext.Current to get Session Variable value. Here my code fails.
Thread
on which the process is running does not have HTTPContext.Current object.
How can I pass-on HTTPContext.Current info to thread.

You can't and you shouldn't.

Your extra thread should not touch HttpContext or anything it refers to,
including the Page. This is because, as you've discovered (the hard way),
once the request is finished, the HttpContext and everything it refers to is
destroyed.

However, if your thread only needs data passed into it, and does not change
that data, then you can do the following:

1) Define a separate class called, for instance, MyThreadClass.
2) Define read-write properties for every value you want passed from the
page into the thread.
3) Define a private method in the class called, for instance, Run. This will
be used as your ThreadStart delegate.
4) Define a public method in the class called, for instance, StartThread.
This method will create the Thread and use "Thread.Start(new
ThreadStart(Run))" to start the thread.
5) When you want to start the thread, create an instance of MyThreadClass,
set the properties you need, then call StartThread.

This will work as long as you don't pass anything referenced by the
HttpContext. For instance, do not pass "Session". It's ok to pass something
which you've stored in Session state, but you can't pass Session itself (nor
Request, nor Response, etc.)

John Saunders
 
D

Dietmar

Boy, this took me a while to figure it out. I needed my separate threa
to work on some variables for the progress report on some email actio
that takes forever. Of course, I needed the emailing to be done in
separate thread. The cleanest way would have been to put everything i
a separate class and initiate it for the New Thread, right? Wrong. I
only works when running on the same page from wich you are calling th
action in a new Thread. Somehow, this maintains the HttpContext.Curren
and keeps it available (or something like this) even in a New Threa
whereas if you do the same in a separate class it doesn't work. I kne
it had to work out somehow given this nice article on threading:
http://www.sitepoint.com/article/threading-asp-net[/url - Dietma
 

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

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,157
Latest member
MercedesE4
Top