简体   繁体   中英

thread.sleep is not working propely

My Purpose:Creating an imagebutton once it is clicked the consumer ought to wait 4 seconds while he is waiting images on the top of the webpage should change automaticly. What I tried so far:

protected void Page_Load(object sender, EventArgs e)
{          
}

protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
    int counter = 0;
    while (counter < 4)
    {
        counter++;
        Image2.ImageUrl = "../images/" + counter + ".jpg";
        System.Threading.Thread.Sleep(1000);
    }

    Response.Redirect("../Home.aspx");
}

I haven't got the same consequences as mentioned earlier. anyone have either solution for solving the problem or a new idea? thnx in advance

Your code makes the web application sleep for 4 seconds, effectively delaying serving the page for 4 seconds. The page is only served after the ImageButton1_Click method is finished.

You should use Javascript to accomplish your purpose.

The problems with your code:

  1. You're changing an Image Url on the server side. This will not show in the client until all data is served to the client.
  2. The four second delay happens on the server side.
  3. You are redirecting to ../Home.aspx . This will "destroy" the Url change you just made.

To solve these problems:

  • Use Javascript/JQuery to delay for 4 seconds and alter the image url on the client side.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM