Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Time delay before redirect

Tags:

c#

timedelay

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
        {
            ...
        }
    }
like image 896
user1331344 Avatar asked Apr 13 '12 13:04

user1331344


People also ask

How do I redirect after delay?

If you want to redirect a page automatically after a delay then you can use the setTimeout() method in JavaScript and it will call the above property after a time delay. The solution is very simple. The setTimeout() method will call a function or execute a piece of code after a time delay (in millisecond).

How do I redirect after 5 seconds?

To redirect a webpage after 5 seconds, use the setInterval() method to set the time interval. Add the webpage in window. location. href object.

How do you delay a link?

To delay a link with inline javascript, just set your href attribute as href="javascript:setTimeout(()=>{window. location = 'URL' },500);" . When you replace the URL with your link, just make sure it is inside the ' ' .

Does response redirect stop execution?

When you use Response. Redirect("Default. aspx",true ) which is by default true then the execution of current page is terminated and code written after the Response. Redirect is not executed instead of executing code written after the Response.


1 Answers

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.

like image 107
lorond Avatar answered Oct 17 '22 22:10

lorond