C# 控制台暂停方式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13661890/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Ways of console pausing
提问by RnD
As I'm new to C#, I searched Google for various stuff which I used to use in C++. One of them is a pause possibility in a console app.
因为我是 C# 的新手,所以我在 Google 上搜索了我曾经在 C++ 中使用的各种东西。其中之一是控制台应用程序中的暂停可能性。
A lot of people suggested different ways like
很多人提出了不同的方法,比如
System.Console.ReadKey(true);
System.Console.WriteLine();
Others even showed self-made functions which 'should' be more efficient than others. And that's a real headache to decide which one is a better solution.
其他人甚至展示了“应该”比其他人更有效的自制功能。决定哪一个是更好的解决方案确实令人头疼。
Could anyone give any examples of how C# interpret them and which way should be the most efficient?
任何人都可以举例说明 C# 如何解释它们以及哪种方式应该是最有效的?
回答by Picrofo Software
I usually use doand whileto pause the console. Then, if necessary, the console should resume if you press a specific key.
我通常使用do和while来暂停控制台。然后,如有必要,如果您按下特定键,控制台应恢复。
Example
例子
do
{
/* while (!Console.KeyAvailable) //Continue if pressing a Key press is not available in the input stream
{
//Do Something While Paused
}
*/
} while (Console.ReadKey(true).Key != ConsoleKey.Escape); //Resume if Escape was pressed
If you leave this as //Do Something While Paused, the console will only resume if the Esckey was pressed doing nothing while paused.
如果您将其保留为//Do Something While Paused,则控制台只会Esc在暂停时按下键不执行任何操作时才会恢复。
However, if you would not like the console application to resume, you can use while (true);instead of while (Console.ReadKey(true).Key != ConsoleKey.Escape);
但是,如果您不希望控制台应用程序恢复,则可以使用while (true);代替while (Console.ReadKey(true).Key != ConsoleKey.Escape);
Example
例子
do
{
//break; //Resume
} while (true); //Continue while True (Always True)
Notice: The console application will pause because by doing do { } while (Condition);you are simply telling the console application that you are doing something. So, the console application will wait for the operation to execute. Then, normally close when there's nothing to do.
Notice: The whileis used to loop. So, the application will not close unless the condition becomes false.
注意:控制台应用程序将暂停,因为这样做do { } while (Condition);您只是告诉控制台应用程序您正在做某事。因此,控制台应用程序将等待操作执行。然后,通常在无事可做时关闭。
注意:while用于循环。因此,除非条件变为假,否则应用程序不会关闭。
Thanks,
I hope you find this helpful :)
谢谢,
我希望你觉得这有帮助:)
回答by Pat Hermens
If you're talking about the built-in "pause" command, you could always call it -- even though it's ugly. Here's what I use:
如果你在谈论内置的“暂停”命令,你总是可以调用它——即使它很丑陋。这是我使用的:
static void Pause()
{
Console.WriteLine();
var pauseProc = Process.Start(
new ProcessStartInfo()
{
FileName = "cmd",
Arguments = "/C pause",
UseShellExecute = false
});
pauseProc.WaitForExit();
}
Normally like so:
通常像这样:
if (Environment.UserInteractive())
Pause();
Hope this helps.
希望这可以帮助。
回答by Pat Hermens
Or you can use what Pat did but for Arguments instead of
或者你可以使用 Pat 所做的,但用于 Arguments 而不是
Arguments = "/C pause"
you can use
您可以使用
Arguments = "/C TIMEOUT /t 4 /nobreak > NUL"
where number 4 is number of seconds console will pause before executing rest of the program.
其中数字 4 是控制台在执行程序的其余部分之前暂停的秒数。
And the whole function would be
整个功能将是
static void Pause(int sec)
{
Console.WriteLine();
var pauseProc = Process.Start(
new ProcessStartInfo()
{
FileName = "cmd",
Arguments = "/C TIMEOUT /t " + sec + " /nobreak > NUL",
UseShellExecute = false
});
pauseProc.WaitForExit();
}
and you will call it with Pause function with number of seconds to pause.
并且您将使用暂停功能调用它并暂停秒数。
Pause(4);
Hope it helps.
希望能帮助到你。
回答by Momina Idrees
you can write "Console.ReadLine();" this too for pupose.
你可以写“Console.ReadLine();” 这也是为了目的。
回答by static_cast
Run the program using any of the following methods:
使用以下任一方法运行程序:
- ctrl+ F5
- ctrl+ F5
OR as Rajesh suggested
或者像拉杰什建议的那样
Console.ReadKey() //pauses for any keyConsole.ReadLine() //pauses for enter key
Console.ReadKey() //pauses for any keyConsole.ReadLine() //pauses for enter key

