当用户按下转义键时,如何 "Pause" 控制台应用程序?
How do I "Pause" a console application when the user presses escape?
我正在创建一个将执行无限进程的 C# 控制台应用程序。当用户按下转义键时,如何让应用程序进入 "pause"?
一旦用户按下转义键,我希望选择退出应用程序或从停止的地方继续循环。我不想在这个过程中出现任何中断。如果我在第 100 步按 Esc
,我应该能够在第 101 步重新开始。
到目前为止,这是我的方法:
// 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());
}
有没有办法在单独的线程中检查用户输入键,然后在另一个线程看到 Esc
按键时暂停无限循环?
要找出循环中是否有可用的键,您可以这样做:
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 如果输入流中有一个键可以读取并且它是一个非阻塞调用所以它不会暂停以等待输入,则为真。您可以检查是否按下了转义键并暂停,或者如果条件为真则执行任何您想做的事情。
罗恩,非常感谢你的回答。使用 Console.KeyAvalible
是找到我的问题的确切解决方案的关键。这是我为产生预期结果所做的工作。我需要添加一些方法来检查用户是否想中断当前操作或从头开始一个新循环。
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;
}
结果如下:
我正在创建一个将执行无限进程的 C# 控制台应用程序。当用户按下转义键时,如何让应用程序进入 "pause"?
一旦用户按下转义键,我希望选择退出应用程序或从停止的地方继续循环。我不想在这个过程中出现任何中断。如果我在第 100 步按 Esc
,我应该能够在第 101 步重新开始。
到目前为止,这是我的方法:
// 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());
}
有没有办法在单独的线程中检查用户输入键,然后在另一个线程看到 Esc
按键时暂停无限循环?
要找出循环中是否有可用的键,您可以这样做:
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 如果输入流中有一个键可以读取并且它是一个非阻塞调用所以它不会暂停以等待输入,则为真。您可以检查是否按下了转义键并暂停,或者如果条件为真则执行任何您想做的事情。
罗恩,非常感谢你的回答。使用 Console.KeyAvalible
是找到我的问题的确切解决方案的关键。这是我为产生预期结果所做的工作。我需要添加一些方法来检查用户是否想中断当前操作或从头开始一个新循环。
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;
}
结果如下: