Search code examples
c#winformsdelayshortcuttiming

How to delay code kind of like "ping localhost..." except in C#


Is it possible to run some code in C# , wait a few seconds, then keep going? I know you can use a timer but is there an easier, quicker way? Like in batch you can do:

echo Hello
ping localhost -n 10 > nul
echo World
pause > nul

So can I do this in C# , and would it work with Winforms or just Console, because I need it for Winforms... [edit] i need to have only a single method to pause, but the rest of the app still continue. For example if i have a button, click, and 5 seconds later continue the rest of the method that was called. Yet in the meantime, still be able to push other buttons?


Solution

  • Begininvoke?

    new Action(() => {
        Console.WriteLine("Waiting...");
        Thread.Sleep(10000);
        Console.WriteLine("Over");
    }).BeginInvoke(null, null);