问题描述
我为我的Web应用程序创建一个注册页面.该应用程序要求用户成功注册一个新帐户后,该页面将显示一条消息注册成功",然后等待5秒钟,然后切换到登录"页面.我使用了Thread.Sleep(5000)
.它等待5秒钟,但不显示该消息.谁能帮我吗?
I create a register page for my web application. The application require that after user successfully register a new account, the page will show a message "Register successfully", then wait for 5 seconds before switch to Login page. I used Thread.Sleep(5000)
. It wait for 5 seconds but it does not display the message. Can anyone help me?
void AccountServiceRegisterCompleted(object sender, RegisterCompletedEventArgs e)
{
if (e.Result)
{
lblMessage.Text = "Register successfully";
Thread.Sleep(5000);
this.SwitchPage(new Login());
}
else
{
...
}
}
推荐答案
Thread.Sleep(5000)
仅将您的线程挂起5秒钟-在这段时间内将不执行任何代码到该线程上.因此没有消息或其他任何内容.
Thread.Sleep(5000)
only suspends your thread for 5 seconds - no code onto this thread will be executed during this time. So no messages or anything else.
如果它是ASP.NET应用程序,则客户端不知道服务器上正在发生什么,并等待服务器的响应5秒钟.您必须手动实现此逻辑.例如,使用JavaScript:
If it's an ASP.NET app, client doesn't know what's going on on server and waits server's response for 5 seconds. You have to implement this logic manually. For example, either using JavaScript:
setTimeout(function(){location.href = 'test.aspx';}, 5000);
或添加HTTP标头:
Response.AddHeader("REFRESH","5;URL=test.aspx");
或meta
标签:
<meta http-equiv="refresh" content="5; url=test.aspx" />
请参见更多信息.
如果它是桌面应用程序,则可以使用计时器.并且永远不要使主线程(UI线程)挂在诸如Thread.Sleep之类的地方.
If it's a desktop application you could use something like timers. And never make main thread (UI Thread) hangs with something like Thread.Sleep.
这篇关于重定向之前的时间延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!