Session serialization error when threading

J

Jeremy Cowles

Hi all,

Here is the issue:

On postback, I spawn a new thread using the following code:

Dim NewThread As New Thread(AddressOf ProcessFile)
NewThread.Priority = ThreadPriority.Lowest
NewThread.Start()
Response.Redirect("loading.aspx")

ProcessFile() is a method in the code-behind class of the currently
executing page. After the thread spawns, any time I access the Session
object I get the following exception:

System.Runtime.Serialization.SerializationException


I have fixed the exception by adding the following line to the
Page_Load event:

Response.Cache.SetCacheability(HttpCacheability.NoCache)

This seems totally strange to me, does anyone know why this fixed the
error?

Thanks,
Jeremy
 
B

Bruce Barker

you can not safely access the session from a background thread. asp.net
serializes page requests to the same session object to prevent numerous
contention issues. if you use inproc sessions (never a good idea with
production machines), you could pass a reference to an object stored in the
session to the background thread, but you will have to add code to make it
threadsafe

-- bruce (sqlwork.com)
 
J

Jeremy Cowles

Thanks for the reply, but that doesn't really answer my question...

In my post, I said it's working fine now. I am not having any problems
with reading/writing sessions values. The problem I am having is
understanding why turning off page cache had anything to do with the
exception being thrown. Any thoughts on that?
 

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,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top