473,396 Members | 1,907 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,396 software developers and data experts.

Thread class and Session variable

Hi All,

I am trying to perform a non-CPU bound operation in an asynchronous fashion
using 'Thread' and a delegate. Basically, I am spawning a thread using
ThreadStart and Thread.

My non-CPU bound operation needs to have access to Session variables; Even
though I embedded the state information (which also includes the context
object) in an object and passed the object to the Thread , I am unable to
access the Session variables. The system throws a Null Object Reference error.

Since the thread that I am spawning is not from the CLR thread pool, it is
not able to access the Session variable. Am I right? How can a system thread
get access to the Session variables of the current HTTPRequest?

I hope I articulated my problem. If not, please let me know.

Any pointers will be appreciated!
Nov 26 '06 #1
4 4200
OHM
This example ( albiet in Visual Basic.NET ) seems to work ok. Im not sure
what you are doing differently , or if I have misunderstood you ?

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim myThread As New Threading.Thread(AddressOf mySpawned)

Session("TestMe") = "Hello World"

myThread.Start()

End Sub
Private Sub mySpawned()

Dim s As String = Session("TestMe")

End Sub

"Diffident" <Di*******@discussions.microsoft.comwrote in message
news:32**********************************@microsof t.com...
Hi All,

I am trying to perform a non-CPU bound operation in an asynchronous
fashion
using 'Thread' and a delegate. Basically, I am spawning a thread using
ThreadStart and Thread.

My non-CPU bound operation needs to have access to Session variables; Even
though I embedded the state information (which also includes the context
object) in an object and passed the object to the Thread , I am unable to
access the Session variables. The system throws a Null Object Reference
error.

Since the thread that I am spawning is not from the CLR thread pool, it is
not able to access the Session variable. Am I right? How can a system
thread
get access to the Session variables of the current HTTPRequest?

I hope I articulated my problem. If not, please let me know.

Any pointers will be appreciated!


Nov 26 '06 #2
Hi OHM,

Thanks for your response.

Here is what I am trying to do (in C#):

=====

ThreadStart t = new ThreadStart(LongDBProcess)
// LongDBProcess is a time-consuming process
Thread ts = new Thread(t);
t.start(); //At this point, system thread will be created and not
the CLR thread

====

I need to have access to Session variables inside my LongDBProcess...

private void LongDBProcess()
{
Session["IDCounter"] = i+1; //At this point, I am getting null
reference
}

If I am correct, the thread is not able to access the session. Probably this
thread's scope is outside the Application Domain whereas the session data
will be inside the Application Domain...am I correct?

Thanks for your pointers.

"OHM" wrote:
This example ( albiet in Visual Basic.NET ) seems to work ok. Im not sure
what you are doing differently , or if I have misunderstood you ?

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim myThread As New Threading.Thread(AddressOf mySpawned)

Session("TestMe") = "Hello World"

myThread.Start()

End Sub
Private Sub mySpawned()

Dim s As String = Session("TestMe")

End Sub

"Diffident" <Di*******@discussions.microsoft.comwrote in message
news:32**********************************@microsof t.com...
Hi All,

I am trying to perform a non-CPU bound operation in an asynchronous
fashion
using 'Thread' and a delegate. Basically, I am spawning a thread using
ThreadStart and Thread.

My non-CPU bound operation needs to have access to Session variables; Even
though I embedded the state information (which also includes the context
object) in an object and passed the object to the Thread , I am unable to
access the Session variables. The system throws a Null Object Reference
error.

Since the thread that I am spawning is not from the CLR thread pool, it is
not able to access the Session variable. Am I right? How can a system
thread
get access to the Session variables of the current HTTPRequest?

I hope I articulated my problem. If not, please let me know.

Any pointers will be appreciated!


Nov 26 '06 #3
OHM
Hmm, not sure on this one. If you can do it with vb.net you should be able
to do it somehow with C#, but I cant get it to work either. The delegate is
obviously working ok, but it seems to have lost its connection to the
context.

I think its probably the way the thread has been started, but I dont
normally do that much with threading It's not something which rattles off
the tip of my head.

My suggestion is to repost this question on the C# newsgroup, someone there
is bound to have an answer pretty quick.

"Diffident" <Di*******@discussions.microsoft.comwrote in message
news:92**********************************@microsof t.com...
Hi OHM,

Thanks for your response.

Here is what I am trying to do (in C#):

=====

ThreadStart t = new ThreadStart(LongDBProcess)
// LongDBProcess is a time-consuming process
Thread ts = new Thread(t);
t.start(); //At this point, system thread will be created and not
the CLR thread

====

I need to have access to Session variables inside my LongDBProcess...

private void LongDBProcess()
{
Session["IDCounter"] = i+1; //At this point, I am getting
null
reference
}

If I am correct, the thread is not able to access the session. Probably
this
thread's scope is outside the Application Domain whereas the session data
will be inside the Application Domain...am I correct?

Thanks for your pointers.

"OHM" wrote:
>This example ( albiet in Visual Basic.NET ) seems to work ok. Im not sure
what you are doing differently , or if I have misunderstood you ?

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim myThread As New Threading.Thread(AddressOf mySpawned)

Session("TestMe") = "Hello World"

myThread.Start()

End Sub
Private Sub mySpawned()

Dim s As String = Session("TestMe")

End Sub

"Diffident" <Di*******@discussions.microsoft.comwrote in message
news:32**********************************@microso ft.com...
Hi All,

I am trying to perform a non-CPU bound operation in an asynchronous
fashion
using 'Thread' and a delegate. Basically, I am spawning a thread using
ThreadStart and Thread.

My non-CPU bound operation needs to have access to Session variables;
Even
though I embedded the state information (which also includes the
context
object) in an object and passed the object to the Thread , I am unable
to
access the Session variables. The system throws a Null Object Reference
error.

Since the thread that I am spawning is not from the CLR thread pool, it
is
not able to access the Session variable. Am I right? How can a system
thread
get access to the Session variables of the current HTTPRequest?

I hope I articulated my problem. If not, please let me know.

Any pointers will be appreciated!




Nov 26 '06 #4
You want to use Asynchronous calls here, either with a Delegate, or using the
2.0 version, the Page class supports Asynchronous page methods and the
context can be passed.

Here is an example article that covers some of this:

http://www.eggheadcafe.com/articles/20060918.asp

Peter
--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"Diffident" wrote:
Hi All,

I am trying to perform a non-CPU bound operation in an asynchronous fashion
using 'Thread' and a delegate. Basically, I am spawning a thread using
ThreadStart and Thread.

My non-CPU bound operation needs to have access to Session variables; Even
though I embedded the state information (which also includes the context
object) in an object and passed the object to the Thread , I am unable to
access the Session variables. The system throws a Null Object Reference error.

Since the thread that I am spawning is not from the CLR thread pool, it is
not able to access the Session variable. Am I right? How can a system thread
get access to the Session variables of the current HTTPRequest?

I hope I articulated my problem. If not, please let me know.

Any pointers will be appreciated!

Nov 26 '06 #5

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

Similar topics

15
by: | last post by:
Hi, I want to do things this way: I have a bunch of stuff that I want to keep track of while a user is connected to the site. Maybe 50 little peices of information. So I know I can make 50...
8
by: Tammy B. | last post by:
Hiya - Big puzzler for me. Code Below I create a class. I save it to a session variable. Then, I retrieve the session variable back into a new local variable. I am able to use a method which...
3
by: Geoff Winsor | last post by:
Hi, I am experiencing a problem with recalling a session variable which stores whether a person is logged in to a "members only" section of a website. This area of the site has been working...
4
by: Stephen | last post by:
I have a .NET (1.1 framework) application that is losing a session variable on only a few PC's. The main page is loading up in a frame in a Portal application. On the Page_Load it stores an...
2
by: George | last post by:
Hi In my ASP.NET project, I have a session variable that is assigned on a login page. In another .cs file (in another directory), I'm trying to use if((string)Session == "something" When I...
4
by: Don Miller | last post by:
I am using a Session variable to hold a class object between ASP.NET pages (in VB). In my constructor I check to see if the Session variable exists, and if it doesn't, I create one and populate it...
17
by: Control Freq | last post by:
Hi, Not sure if this is the right NG for this, but, is there a convention for the variable names of a Session variable? I am using .NET 2.0 in C#. I am new to all this .NET stuff, So, any...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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...

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.