Is there a functional C# asp Delay function? RRS feed

  • Question

  • So I thought this would be straight forward but C# or asp or the combination of the two are going to make me pull my hair out.

    Here's what I need to make happen and nothing seems to fix it:

    Do something. (Actually do it too) We'll call this command 1;


    Then do something else. We'll call this command 2;

    In C this would just be one line Delay(int microseconds); , but for some reason that is beyond my comprehension C# doesn't seem to have that.  I have tried every possible combination of Thread.Sleep(int X); and Await Task.delay(int X); and dozens of others.  They literally all do the exact same thing. They wait and then execute Command 1 and command 2 back to back.  There's something basic that I'm missing about C# or asp or both, but after searching forum after forum, blog after blog, and trying all the codes out there that people say work, I am truly desperate for something that actually does exactly this.  If there's anyone out there with the ability to create a simple delay feature please incorporate it into this language or make it easier to find if it is out there. 

    Friday, December 28, 2018 3:49 AM

All replies

  • In a simple Console application, inside Main function, the next fragment prints “Command1”, then waits three seconds and prints “Command2”:

       Console.WriteLine( "Command1" );

       Thread.Sleep( TimeSpan.FromSeconds( 3 ) );

       Console.WriteLine( "Command2" );


    The approach depends on the type of application and other aspects.

    • Edited by Viorel_MVP Friday, December 28, 2018 5:59 AM
    Friday, December 28, 2018 5:57 AM
  • Ok Thank you for your reply.  What if I wanted to do something like a Count down on display on a website.  Like this:



    Thread.Sleep( TimeSpan.FromSeconds( 1 ) );


    Thread.Sleep( TimeSpan.FromSeconds( 1 ) );


    Thread.Sleep( TimeSpan.FromSeconds( 1 ) );


    Thread.Sleep( TimeSpan.FromSeconds( 1 ) );


    I've tried the Thread.Sleep commands and so far it always executes all of the sleeps first and then executes the TextBox commands.  Is there a way to ensure execution order.

    Thank you for your assistance

    Thread.Sleep( TimeSpan.FromSeconds( 1 ) );
    Friday, December 28, 2018 4:33 PM
  • In case of ASP Web applications, the approach seems more complex, because sometimes you have to use JavaScript for Web browsers instead of C# that is on ASP server.

    I suggest asking the ASP specialists: https://forums.asp.net/.

    Probably you can use JavaScript functions like setTimeout, setInterval, getElementById.

    Or you can use a Timer and UpdatePanel controls: https://docs.microsoft.com/en-us/dotnet/api/system.web.ui.timer. Then you do not need JavaScript.

    Saturday, December 29, 2018 7:23 PM