-2

As the title says I want to print these lines with 0.1 seconds between them. What is the easiest way to do that?

            Console.WriteLine("0.2seconds");
            Console.WriteLine("0.3seconds");
            Console.WriteLine("0.4seconds");
            Console.WriteLine("0.5seconds");
            Console.WriteLine("0.6seconds");
            Console.WriteLine("0.7seconds");
            Console.WriteLine("0.8seconds");
            Console.WriteLine("0.9seconds");
            Console.WriteLine("1second");
  • `System.Threading.Thread.Sleep(milliseconds)`, https://stackoverflow.com/questions/91108/how-do-i-get-my-c-sharp-program-to-sleep-for-50-milliseconds – K.Cl Oct 01 '22 at 18:44
  • `away Task.Delay(milliseconds);` declare the method as `async` – Dmitry Bychenko Oct 01 '22 at 18:46
  • Does this answer your question? [C# WaitFor or Pause for X Seconds Before Next Line](https://stackoverflow.com/questions/12385110/c-sharp-waitfor-or-pause-for-x-seconds-before-next-line) – gunr2171 Oct 01 '22 at 18:59

1 Answers1

0

You can StopWatch() and Thread.Sleep(100) to print at every 0.1 seconds.

private static void Main(string[] args)
{
    var stopwatch = new Stopwatch();
    stopwatch.Start();
    
    while (true)
    {
        Thread.Sleep(100); // 0.1 sec delay
        var elapsedSeconds = 
            Math.Round((double)stopwatch.Elapsed.TotalMilliseconds/1000, 1); // round to 1 decimal place
        Console.WriteLine($"{elapsedSeconds}seconds");
    }
}
Ibrahim Timimi
  • 2,656
  • 5
  • 19
  • 31