473,418 Members | 2,138 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,418 software developers and data experts.

HTTPContext in Threading

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
Nov 19 '05 #1
4 7892
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.
"Makarand Keer" <Ma**********@discussions.microsoft.com> wrote in message
news:B1**********************************@microsof t.com...
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

Nov 19 '05 #2
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

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 19 '05 #3
"Makarand Keer" <Ma**********@discussions.microsoft.com> wrote in message
news:B1**********************************@microsof t.com...
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
Nov 19 '05 #4

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:
[url]http://www.sitepoint.com/article/threading-asp-net[/url
-
Dietma
-----------------------------------------------------------------------
Posted via http://www.codecomments.co
-----------------------------------------------------------------------

Nov 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Peter Kirk | last post by:
Hi there I have a program written by another company (it's a "web control" which returns a web-page: can I compare this to a servlet in the Java world?), which they think is causing problems on...
2
by: Claudio Biagioli | last post by:
I start a parallel thread inside a control with the following code: Private Sub StartParallelProc(ByVal Command As SqlClient.SqlCommand) Dim T As New Threading.Thread(AddressOf ParallelProc) ...
10
by: Roberto López | last post by:
Hi, I´m doing an asp.net application that uploads and downloads files and folders between the client and the server on my intranet. To do this I have create threads and it runs Ok but I need to...
3
by: Daniel Maycock via .NET 247 | last post by:
I can't get my threading timer to show a splash screen panel for six seconds, then move onto the next panel. The timer just doesn't tick (this is aparent when I set the time to wait to 100 and it...
2
by: Gavin Lyons via .NET 247 | last post by:
Hello, I'm writing a newsletter application which uses backgroundthreading. I'm using Session variable to report on progresswhile it loops through a dataset. The 'Status.aspx' pagerefreshes every...
3
by: xzzy | last post by:
A. I need to access: System.Web.HttpContext.Current.Cache but get this error: "Object reference not set to an instance of the object" B. System.Web.HttpContext.Current.Cache is accessible...
1
by: Dave | last post by:
I'm trying call a couple WebRequests asynchronously using the QueueUserWorkItem, but one thing I noticed is that the HttpContext is null within the asynchronous call. I was using...
0
by: Aaron Morton | last post by:
I'm working on a IHttpModule that handles the PreSendRequestHeaders event from the HttpApplication, if the event is raised after EndRequest then HttpContext.Current is null. If it is raised before...
0
by: LoneTiger | last post by:
Hi everyone, I've got a smaller problem which I can't get around of. I've seen some quite usefull answers around and was hoing someone might be able to help. What I'm working on is a...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.