By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,786 Members | 1,131 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,786 IT Pros & Developers. It's quick & easy.

Thread class and Session variable

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.