当用户按下“Escape”键时,我该如何“暂停”控制台应用程序?

7
我正在创建一个C#控制台应用程序,它将执行一个无限过程。当用户按下Escape键时,我该如何使应用程序“暂停”?
一旦用户按下Escape键,我想要选择退出应用程序或者继续循环,但不希望在处理过程中出现任何不连续。如果我在第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 键时暂停无限循环?


所以你要找的是一种检查是否按下了转义键的方法,但又不想让循环等待用户输入,对吗? - Maor Veitsman
1
嗯,用户很可能不会在没有手册的情况下找到按这个键的方法。他只需要按Ctrl+S即可,不需要帮助。 - Hans Passant
1
@AlexeiLevenkov,我认为他想要做的是在不暂停输入的情况下检查 ESC 键。 - Ron Beyer
1
看一下 Console.KeyAvailable,然后你可以使用 Console.ReadKey 来读取它。这将允许你在循环内部检查而不需要显式地要求用户输入。 - Ron Beyer
1
@RonBeyer 听起来对我来说就是一个答案 - BradleyDotNET
显示剩余3条评论
2个回答

11

要查找循环中是否有可用的关键字,可以执行以下操作:

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 会返回 true,如果有一个键已经准备好读取并且它是非阻塞的调用,因此它不会暂停等待输入。如果条件为真,您可以检查是否按下了 Escape 键并暂停或执行任何您想要的操作。


0
Ron,非常感谢您的回答。使用Console.KeyAvailable是找到解决方案的关键。以下是我为了得到期望结果所做的事情。我需要添加一些方法来检查用户是否想要中断当前操作或从头开始启动新循环。
    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;
    }

以下是结果:

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接