C# while loop Running until user press key

  1.  
        
    static void CancelDemo()
            {
                int i = 0;
                //Console.WriteLine("Press ESC to stop");
    
                //while (!(Console.KeyAvailable && Console.ReadKey(true).Key == ConsoleKey.Escape))
                //{
                //    Console.WriteLine(++i);
                //}
                Console.WriteLine("Enput any key to stop console output!");
                while(!Console.KeyAvailable)
                {
                    Console.WriteLine(++i);
                }
            }
     
        

     

    static void CancelDemo()
            {
                int i = 0;
                Console.WriteLine("Press ESC to stop");
    
                while (!(Console.KeyAvailable && Console.ReadKey(true).Key == ConsoleKey.Escape))
                {
                    Console.WriteLine(++i);
                }
            }

     

你可能感兴趣的:(C# while loop Running until user press key)