C#C
C#3y ago
SWEETPONY

✅ can Thread.Sleep affect performance?

namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Press any key for one second...");

            DateTime endWaitTime = DateTime.Now.AddSeconds(1);
            while (DateTime.Now < endWaitTime)
            {
                if (Console.KeyAvailable) 
                {
                    var key = Console.ReadKey(true); 
                    Console.WriteLine($"You pressed: {key.KeyChar}");
                    break;
                }
                Thread.Sleep(50); // a short pause to reduce the CPU load
            }

            Console.WriteLine("Continuation of the program execution...");
        }
    }
}


Should I add Thread.Sleep(50); in this situation? Is it worth it?
Was this page helpful?