c# - ASP.Net Web App: How to pass Session to new thread? -


i have web application that, based on input user, calculates combination of products fit requirements , returns quote user. calculation can take time, displaying progress dialog user.

the progress dialog usercontrol updatepanel, contains text labels, button, , timer used asynchpostbacktrigger trigger. declared on aspx page hidden default.

before, created separate thread heavy work while updating progress dialog using main thread. there no issues there. here start thread:

    protected void startquotecalculation(object sender, eventargs e)     {         totalpanelcount = currentquote.panelcount;         progressdialog.showpopup();          calculatequotethread = new thread(new threadstart(calculatequote));         calculatequotethread.start();     } 

where calculatequote heavy work.

however, i've had change unrelated exactly, affecting calculation of combinations.

a variable that's used during quote calculation stored in user session. session lost when start worker thread above when accessing variable throwing nullreference exception. fix this, i've tried passing httpcontext.current new thread so:

protected void startquotecalculation(object sender, eventargs e) {         totalpanelcount = currentquote.panelcount;         progressdialog.showpopup();           httpcontext ctx = httpcontext.current;          calculatequotethread = new thread(new threadstart(() =>              {                  httpcontext.current = ctx;                  calculatequote();              }));          calculatequotethread.start(); } 

however, session seems stay valid short while, after few seconds still throwing nullreferenceexception. httpcontext.current valid, httpcontext.current.session null.

so, have few questions:

  1. if it's possible pass httpcontext other thread, why session going null after short time? how can prevent this?
  2. is possible create, display , update progressdialog panel in new thread? (to keep product calculation in main thread valid session data)

i've tried couple things on #2, not knowledgable on asp.net , web development in general, haven't been successful.

rather storing individual variables in session store entire object contains state of calculation. way website polling status updates application can retrieve current state retrieving object session. it's going "in progress," "complete" (the output), or exception (never happens!)

if want users able run multiple processes store dictionary<guid, yourprocess> , client receive guid , use request status.

this way client has reference object executing process, thread executing process doesn't need reference session or httpcontext. needs know instance of class it's operating on, , update either progress or final result.


Comments

Popular posts from this blog

java - Static nested class instance -

c# - Bluetooth LE CanUpdate Characteristic property -

JavaScript - Replace variable from string in all occurrences -