Threading and Session Objects

  • Thread starter Thread starter Roberto López
  • Start date Start date
R

Roberto López

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 show to the user the progress of
the operation and here is the problem. I try to access to the Session
Variables from inside the thread to show it on a page that automatically
refresh, BUT, I have discovered that there is no access to the httpcontext
from inside the new thread. Is there any other way to do this??

Thanks a lot.
 
I just readed this article but, what i need is to access to the session
object from within a class. And doing it I alwais receive an error.
 
The same error appears. For some reason, when the new thread is started from
a class, the httpcontext is set to null to the thread. If it is done from a
aspx page there is no error. Why? I don´t know.
 
Perhaps you could supply a reproducer? I don't immediately see why there
would be a problem passing Page.Context to the new thread before it starts.
In particular, I can't imagine how, if you passed a non-null HttpContext
into the thread, it would become null. I can understand how it could become
invalid, but not how it could become null.
 
The problem is that I not only need to read values from session variables,
i need to update session variables while the new thread is running, and here
is the problem. When i try to set a new value to a session variable I
receive
the error of HttpContext is a Null Reference.
I need to store a value into the session variable to read it periodically
from other
page to know about the status of the process. I hope you understand me.

My code is like this:

public class AccessIO

public sub New()

end sub

public sub CopyFiles()
...routine to copy files...
...... ..... ... ..


...here is the error...
httpcontext.current.session("PercentComplete") = anyvalue

end sub

end class
 
Roberto, don't do that.

Instead, house your thread in a separate class. Give that class a private
member variable of type HttpContext, say, m_Context. Write a public property
of type HttpContext say, TheContext.

To use the thread, create an instance of the thread class. Then use that
instance to set the property:

myThread.TheContext = Page.Context

Then start the thread. The thread can then access m_Context instead of
HttpContext.Current.
 
Although I don't know what will the performance impact be, but I think
this should work:

In your main thread, the thread have valid permission to access/modify
Session, you can process the events raised by the threads and do
Invokes inside the handlers, then you can do what ever you want.

Therefore, create some events for the theads (I assume you use a class
to wrap the thread function).

Hope it helps,
Homa Wong
 
I don´t understand clearly the code you write, but, with this, can I write
new values in session variables and these are readable from other page while
the thread is running?
 
Back
Top