14

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
        {
            ...
        }
    }
Dyrandz Famador
  • 4,499
  • 5
  • 25
  • 40
user1331344
  • 163
  • 1
  • 1
  • 4
  • 4
    ASP .NET WebForms? MVC? Other? Please tag appropriately as it affects the answers you'll receive. – Yuck Apr 13 '12 at 13:15
  • Why do you not redirect immediately to the register page, with a flag which says 'Register Successful'; Redirecting with a GET parameter of ?register=true or something to show the flag is enough. This is also much more user friendly. – Bowersbros Oct 06 '14 at 12:24

2 Answers2

35

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.

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);

or by adding HTTP header:

Response.AddHeader("REFRESH","5;URL=test.aspx");

or meta tag:

<meta http-equiv="refresh" content="5; url=test.aspx" />

see more info.

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.

NoWar
  • 36,338
  • 80
  • 323
  • 498
lorond
  • 3,856
  • 2
  • 37
  • 52
0

Only meta tag is enough to redirect to another page

ad meta tag dynamically

Response.AddHeader("REFRESH", "5;URL=~/account/login");

This code will ad a meta tag to current page and your page will redirect to another page in specified time like above.

Sunil Acharya
  • 1,153
  • 5
  • 22
  • 39