问题描述
这是场景.我有一个名为ClientPage的网页.当用户单击ClientPage中的按钮时,会发生2件事-
1)调用一个运行函数的线程.我们称它为thread1.
2)将用户定向到播放动画的WaitPage.同时thread1继续运行.
完成thread1后,将自动假定WaitPage知道该信息并将用户重定向到另一个页面.我该如何实现?
我的计划是让ClientPage存储会话变量->
Session["State"]="Started";
然后,WaitPage将启动一个线程,比如说thread2,它将继续检查Session["State"]
的状态.线程1完成后,它将把Session["State"]
状态更新为已完成".然后,WaitPage将知道并将用户重定向到适当的新页面.
我现在的问题是线程无法正确访问会话变量.即使我将当前的HttpContext传递给线程,该线程也无法访问它,因为原始HttpContext在调用该线程后不久就会过期.我无法保留原始的HttpContext,因为我的WaitPage需要继续加载.对于不知道我在说什么的人,请看下面的代码.我正在谈论做如下WaitPage->
中所示的操作
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->
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
}
}
因此,如上所示,此操作无效.
我也无法在WaitPage中执行以下操作,因为这样Wait 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->
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.
我该如何实现自己想做的事?你能帮帮我吗?
如果您不明白我要达到的目标,请让我知道您到底不明白什么,我会尽力改善我的问题.
谢谢
Jobin
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
推荐答案
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>
继续通过Ajax/javascript重新加载您的waitPage或只是在meta标签中刷新页面.
在waitPage中,找到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");
}
}
这篇关于如何使用线程继续检查页面中的会话变量. ASP.NET专家有人吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!