asp.netc#-4.0sessionsession-state-server

Asp .net session variables update by thread is not getting reflected in Session


In my page1.aspx i am generating a report from database by using thread.

//on button click
Hashtable ht = (Hashtable)Session["ReportParam"];
ReportThreadClass rth = new ReportThreadClass(ht);
Thread thread = new System.Threading.ThreadStart(rth .Run);
thread.Start();

In my thread class's rum method i am updating values in Hashtable that how many pages i have created.

//in thread' method        
public virtual void Run()
{      
    int pagecount=0;
    while(done)
    {
        //loading data from DB and generating html pages

        ht["Total_Pages"] = pagecount;
    }
}

At my Page2.aspx i am reading values from Session Variable

Hashtable ht = (Hashtable)Session["ReportParam"];
int TotalPages = (int) ht["Total_Pages"];

When i run above code in InProc mode every thing is working fine i am getting updated values from session. Because every thing is stored in static variable, and ht is referenced by Session so it automatically get updated in session (HashTable not needed to reassign it to session back).

But when i run code in State server (OutProc mode) It need to store session data in different process by Serializing Hash-table.

But the value of Total_Pages is not getting updated in Page2.aspx even after Thread run completely.

So is there any event or method which get fired to store all updates in session variable to State-Server , if yes then pls tell me . if not then pls suggest me some idea to get updated value in page2.aspx.


Solution

  • In Out Proc Mode Session is saved after some event so if your thread is updating your session variables then it won't persist in storage.

    If u are using Inproc Mode then session store in Static Dictionary so if your thread updating it, u will get updated value to any page.

    So u have two solutions for this situation

    1. Use inProc mode
    2. Maintain a dictionary in your thread class with key as Session id and value is your hash-table, So if page2.aspx wants to read value of hash-table then it will pass his session id to method and which will return required value.