Frage

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.

War es hilfreich?

Lösung 2

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.

Andere Tipps

I would explictely SET and GET SessionState like so:

In your thread Run

// no complex object like hastable, just a plain value...
Session["pageCount"] = pageCount;

In your page2.apsx:

var pageCount = (int) Session["pageCount"]??0;

The reason your report thread is not updating it's session value when using out-of-proc sessionstate is because the session has no way to detect the hashtable has a changed value, therefor it doesn't update the underlying storew with the serialized version of the hastable. When you explicity store one immutable object it will persist one it's value changed;

As the session might already be gone when your thread finishes a begtter option is to get hold of a reference to SqlSessionStateStore and call SetAndReleaseItemExclusive. Ultimately you might want to have an overloaded SessionStateProvider that can handle your scenario.

Less efficient but I'd probably just ping the database for the page count on Page2.

Or create a separate session value for the page count on Page1, at the same time as doing everything else. (EDIT: Nevermind the second part, that's what Rene suggested below).

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top