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?
source share