3

I would like to display a message for a few seconds before moving on to executing code that follows. How can I do this in C#?

page load ()....
{
    label1.text="Thank you for your input";
    Delay by 3 seconds??? before entering codeX
    codeX....
    code....
}

Is it as simple as using a wait(3000)?

Clarifying my Question: I am creating a survey web part in sharepoint and would like to display the message "Thanks for your vote" for 3 seconds before rendering the results of the survey. I am assuming I could use the timer inside of an AJAX update panel, that way It wouldnt affect the rest of the page? I hope this clarifies my Question. Please let me know, the best way I can implement this. Thanks

user1266515
  • 796
  • 3
  • 15
  • 33

3 Answers3

5

You don't want to do what you think you want to do. Not only that; what you want to do does not do what you think it does.

Server handler threads are a finite resource. You should never purposefully make them wait. You should also never purposefully make a user wait.

That said, doing what you are asking for won't do what you think it will, anyway; It will delay the whole page three seconds - it will not just display one thing and wait to display the rest.


What you are probably seeking instead is to update the page via AJAX, 3 seconds after the page loads. That would be initiated by client javascript. We'd need to know more what you are trying to do to point you in a better direction, there.

Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
  • Yes you are right. That is exactly what I would like to do. I would like to put all my asp:controls inside an AJAX update panel and then use the timer. How can I do this? – user1266515 Apr 03 '12 at 14:41
2

Use Javascript to show and hide your message.

<script type="text/javascript">
   $(document).ready(function () {
       $(".message").show();
       $(".message").delay(5000);
       $(".message").fadeOut(2000);
   });
</script>

with a div on your page:

<div class="message">Thanks for your input</div>

this can be on your redirected page. Or you can call the function on the curernt page if your not redirecting, in which case you will need to change the ready call to your own call.

IAmGroot
  • 13,760
  • 18
  • 84
  • 154
0

Thread.Sleep(num of milliseconds) on server-side

or on client-side

setTimeout(,1250);

Boris Modylevsky
  • 3,029
  • 1
  • 26
  • 42