How to "Pause" the console application when the user clicks the "Escape" button?

I am creating a C # console application that will execute an endless process. How can I make the application "pause" when the user presses the evacuation key?

As soon as the user presses the evacuation key, I want to either exit the application or continue the cycle to the right, where he left off. I do not want to interrupt the process. If I press Esc in step 100, I should be able to select the right backup in step 101.

Here is my method:

  // Runs the infinite loop application public static void runLoop() { int count = 0; while (Console.ReadKey().Key!= ConsoleKey.Escape) { WriteToConsole("Doing stuff.... Loop#" + count.ToString()); for (int step = 0; step <= int.MaxValue; step++ ) { WriteToConsole("Performing step #" + step.ToString()); if (step == int.MaxValue) { step = 0; // Re-set the loop counter } } count++; } WriteToConsole("Do you want to exit? y/n"); exitApplication(ReadFromConsole()); } 

Is there a way to check the user input key in a separate thread, and then pause the endless loop when another thread sees the Esc key pressed?

+6
source share
2 answers

To find out if there is a key available in the loop, you can do this:

 while (someLoopCondition) { //Do lots of work here if (Console.KeyAvailable) { var consoleKey = Console.ReadKey(true); //true keeps the key from //being displayed in the console if (consoleKey.Key == ConsoleKey.Escape) { //Pause here, ask a question, whatever. } } } 

Console.KeyAvailable returns true if there is a key in the input stream that is ready for reading, and this is a non-blocking call so that it wins. Do not stop to wait for input. You can check if the escape key is pressed, pause or do what you want if the condition is true.

+10
source
Ron, thank you very much for your reply. Using Console.KeyAvalible was the key to finding the exact solution to my problem. Here is what I did to get the expected results. I needed to add several methods to check if the user wants to break the current operation or start a new cycle from the very beginning.
  public static void runUpdater() { int count = 0; while (true) { WriteToConsole("Doing stuff.... Loop# " + count.ToString()); for (int step = 0; step <= int.MaxValue; step++) { if (!breakCurrentOperation()) { WriteToConsole("Performing step #" + step.ToString()); if (step == int.MaxValue) { step = 0; // Re-set the loop counter } } else { break; } } count++; if (!startNewOperation()) { // Noop } else { break; } } WriteToConsole("\nAre you ready to run the database updater again? y/n"); startApplication(ReadFromConsole()); } public static bool startNewOperation() { WriteToConsole("Do you want go back to the main menu or start a new update process? \nType y to start a new update process or n to go to the main menu."); string input = ReadFromConsole(); if (input == "y" || input == "Y") { return false; } else if (input == "n" || input == "N") { return true; // Noop - Restart the Loop from the begining } else { WriteToConsole("Error: Input was not recognized. "); return startNewOperation(); // Recursivly call method untill user enters a logical input } } public static bool breakCurrentOperation() { if (Console.KeyAvailable) { var consoleKey = Console.ReadKey(true); if (consoleKey.Key == ConsoleKey.Escape) { WriteToConsole("Do you want to stop the current process? \nType s to stop or c to continue."); string input = Console.ReadLine(); if (input == "c" || input == "C") { return false; // Continue } else if (input == "s" || input == "S") { return true; // Break the loop } else { WriteToConsole("Error: Input was not recognized, the current process will now continue. Press Esc to stop the operation."); } } } return false; } 

Here are the results:

enter image description here

0
source

All Articles