Click here to Skip to main content
15,922,015 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is the scenario. I have a webpage called ClientPage . When user clicks on a button in ClientPage, 2 things happen-

1) A thread is called which runs a function. Lets call it thread1.
2) The user is directed to a WaitPage where a animation is played. Meanwhile thread1 keeps running.


When thread1 is done, the WaitPage is automatically supposed to know that and redirect the user to another page. How can I achieve this?

My plan was for ClientPage to store a session variable ->
Session["State"]="Started";

Then the WaitPage would start a thread, lets say thread2, which would keep checking the status of Session["State"]. When thread1 is done, it would update Session["State"] status to "Completed". Then the WaitPage would know and redirect user to appropriate new page.

My problem right now is that session variables cannot be accessed by threads properly. Even if I pass the current HttpContext to the thread, the thread is unable to access it because the original HttpContext expires soon after the thread is called. I cannot hold on the original HttpContext because my WaitPage needs to keep loading. To people who do not know what I am talking about, look at code below. I am talking about doing something as shown below in WaitPage->
C#
private void Page_Load(object sender, System.EventArgs e)
     {
          current_context = HttpContext.Current;
          timer1.Interval = 2000;
          CallNewThreadwithParameter(current_context);//This function is not //provided but understand that this just calls a thread called NewThread with parameter //current_context.
     }//Current context expires here.

    
    NewThread(HttpContext context)
{
      HttpContext newcontext = context;
  if(newcontext.Session["State"].ToString()=="Completed")  //This code does //not work because the current_context expires.
   {
      //     Redirect to another page
   }

}


So as shown above this does not work.

I cannot also do something like below in my WaitPage because then the Wait Page would never load->
C#
private void Page_Load(object sender, System.EventArgs e)
     {
          current_context = HttpContext.Current;
          timer1.Interval = 2000;
          CallNewThreadwithParameter(current_context);//This function is //not provided but understand that this just calls a thread called //NewThread with parameter current_context.
       
          while( current_context[Session]=="Started" )

{
    //My WaitPage will never load.
}

     }//Current context expires here.


How can I achieve what I want to do? Can you help me out?

If you do not understand what I am trying to achieve, please let me know what exactly you dont understand and I will try improve my question.



Thanks
Jobin
Posted
Updated 28-Feb-11 3:22am
v4

You could use client side code to reload the page itself.

Using the client side page load event will run a client side timer and after a period of time, reload itself.

In the server side check the session state using the page load and then redirect when the other thread has finished.

Here is a link to some timer info for javascript.
http://www.elated.com/articles/javascript-timers-with-settimeout-and-setinterval/[^]
 
Share this answer
 
It is clear that Threads cannot access the Session Object as httpContext.current is Expires.

You need a Common Object that is accessible to both waitpage and Thread and also Session specific.
try with static class
public class SharedVar
{
  public static List<threadstatus> L;
}

public class ThreadStatus
{
  public string SessionID;
  public string status;
}

//pass the threadstatus object in your thread..

ThreadStatus t= new ThreadStatus();
t.SessionID = Session.SessionID;
t.status = "started";


// in thread add the object in list.
SharedVar.L.Add(t);


//process the job....... and finally.
t.status = "completed";

</threadstatus>


keep re-loading your waitPage through Ajax/ javascript or simply page-refresh in meta tags.
in waitPage find the status of the Threadstatus Object.

foreach( ThreadStatus t in SharedVar.L)
{
  if(t.SessionID == Session.SessionID && t.status == "completed")
     {
        response.redirect("jobsfinished.aspx");
     }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900