pavan kumar
pavan kumar

Reputation: 408

is it possible to create a session variable within a thread?

Problem is i need to get data from sql server using a stored procedure which usually takes more than 10sec to complete. i was trying to assign this task over to a thread.with in the thread when it gets data,the table is stored in a session variable to be used in the further pages.

Thread thread = new Thread(delegate () {Session["dashboardPatientrecords"]=df.getPatients(); });
thread.Start();

the thread is working alright pulling the data but as go the next page and try to access the session variable like

var dt=(DataTable)Session["dashboardPatientrecords"];

the "dt" is set null

please help

thank you

Upvotes: 0

Views: 1543

Answers (3)

Sam
Sam

Reputation: 2917

Use the Cache instead of Session. It's not safe to use session in threading environment.

Upvotes: 1

Nikhil Vartak
Nikhil Vartak

Reputation: 5137

You can populate a session object with a thread result. Not within a thread though.

DataTable dt = null;

var thread = new Thread(() =>
    {
      dt = df.getPatients();
    });

thread.Start();
thread.Join();

Session["dashboardPatientrecords"] = dt;

Upvotes: 1

Alina Anjum
Alina Anjum

Reputation: 1230

try this ..

DataTable dt=Session["dashboardPatientrecords"] as DataTable;

Upvotes: 0

Related Questions