Listen for key press in .NET console app

后端 未结 9 1449
隐瞒了意图╮
隐瞒了意图╮ 2020-11-22 05:02

How can I continue to run my console application until a key press (like Esc is pressed?)

I\'m assuming its wrapped around a while loop. I don\'t like

9条回答
  •  醉梦人生
    2020-11-22 05:16

    From the video curse Building .NET Console Applications in C# by Jason Roberts at http://www.pluralsight.com

    We could do following to have multiple running process

      static void Main(string[] args)
        {
            Console.CancelKeyPress += (sender, e) =>
            {
    
                Console.WriteLine("Exiting...");
                Environment.Exit(0);
            };
    
            Console.WriteLine("Press ESC to Exit");
    
            var taskKeys = new Task(ReadKeys);
            var taskProcessFiles = new Task(ProcessFiles);
    
            taskKeys.Start();
            taskProcessFiles.Start();
    
            var tasks = new[] { taskKeys };
            Task.WaitAll(tasks);
        }
    
        private static void ProcessFiles()
        {
            var files = Enumerable.Range(1, 100).Select(n => "File" + n + ".txt");
    
            var taskBusy = new Task(BusyIndicator);
            taskBusy.Start();
    
            foreach (var file in files)
            {
                Thread.Sleep(1000);
                Console.WriteLine("Procesing file {0}", file);
            }
        }
    
        private static void BusyIndicator()
        {
            var busy = new ConsoleBusyIndicator();
            busy.UpdateProgress();
        }
    
        private static void ReadKeys()
        {
            ConsoleKeyInfo key = new ConsoleKeyInfo();
    
            while (!Console.KeyAvailable && key.Key != ConsoleKey.Escape)
            {
    
                key = Console.ReadKey(true);
    
                switch (key.Key)
                {
                    case ConsoleKey.UpArrow:
                        Console.WriteLine("UpArrow was pressed");
                        break;
                    case ConsoleKey.DownArrow:
                        Console.WriteLine("DownArrow was pressed");
                        break;
    
                    case ConsoleKey.RightArrow:
                        Console.WriteLine("RightArrow was pressed");
                        break;
    
                    case ConsoleKey.LeftArrow:
                        Console.WriteLine("LeftArrow was pressed");
                        break;
    
                    case ConsoleKey.Escape:
                        break;
    
                    default:
                        if (Console.CapsLock && Console.NumberLock)
                        {
                            Console.WriteLine(key.KeyChar);
                        }
                        break;
                }
            }
        }
    }
    
    internal class ConsoleBusyIndicator
    {
        int _currentBusySymbol;
    
        public char[] BusySymbols { get; set; }
    
        public ConsoleBusyIndicator()
        {
            BusySymbols = new[] { '|', '/', '-', '\\' };
        }
        public void UpdateProgress()
        {
            while (true)
            {
                Thread.Sleep(100);
                var originalX = Console.CursorLeft;
                var originalY = Console.CursorTop;
    
                Console.Write(BusySymbols[_currentBusySymbol]);
    
                _currentBusySymbol++;
    
                if (_currentBusySymbol == BusySymbols.Length)
                {
                    _currentBusySymbol = 0;
                }
    
                Console.SetCursorPosition(originalX, originalY);
            }
        }
    

提交回复
热议问题