C# 为什么控制台窗口在显示我的输出后立即关闭?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8868338/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 04:52:58  来源:igfitidea点击:

Why is the console window closing immediately once displayed my output?

c#.netconsole-applicationmsdn

提问by user962206

I'm studying C# by following the guides in MSDN.

我正在按照MSDN 中的指南学习 C# 。

Now, I just tried the Example 1(hereis the link to MSDN), and I've encountered an issue: why is the console window closing immediately once displayed my output?

现在,我刚刚尝试了示例 1这里MSDN的链接),但我遇到了一个问题:为什么控制台窗口在显示我的输出后立即关闭?

using System;

public class Hello1
{
    public static int Main()
    {
        Console.WriteLine("Hello, World!");
        return 0;
    }
}

采纳答案by Cody Gray

the issue here is that their Hello World Program is showing up then it would immediately close.
why is that?

这里的问题是他们的 Hello World 程序出现然后它会立即关闭。
这是为什么?

Because it's finished.When console applications have completed executing and return from their mainmethod, the associated console window automatically closes. This is expected behavior.

因为已经结束了。当控制台应用程序完成执行并从它们的main方法返回时,相关的控制台窗口会自动关闭。这是预期的行为。

If you want to keep it open for debugging purposes, you'll need to instruct the computer to wait for a key press before ending the app and closing the window.

如果您想让它保持打开状态以进行调试,则需要指示计算机在结束应用程序和关闭窗口之前等待按键按下。

The Console.ReadLinemethodis one way of doing that. Adding this line to the end of your code (just before the returnstatement) will cause the application to wait for you to press a key before exiting.

Console.ReadLine方法是这样做的一种方式。将此行添加到代码末尾(就在return语句之前)将导致应用程序在退出之前等待您按下某个键。

Alternatively, you could start the application without the debugger attached by pressing Ctrl+F5from within the Visual Studio environment, but this has the obvious disadvantage of preventing you from using the debugging features, which you probably want at your disposal when writing an application.

或者,您可以通过在 Visual Studio 环境中按Ctrl+来启动不附加调试器的应用程序F5,但这有一个明显的缺点,即阻止您使用调试功能,而您在编写应用程序时可能希望使用这些功能。

The best compromise is probably to call the Console.ReadLinemethod only when debugging the application by wrapping it in a preprocessor directive. Something like:

最好的折衷方法可能是Console.ReadLine仅在调试应用程序时通过将其包装在预处理器指令中来调用该方法。就像是:

#if DEBUG
    Console.WriteLine("Press enter to close...");
    Console.ReadLine();
#endif

You might also want the window to stay open if an uncaught exception was thrown. To do that you can put the Console.ReadLine();in a finallyblock:

如果抛出未捕获的异常,您可能还希望窗口保持打开状态。要做到这一点,你可以把它Console.ReadLine();放在一个finally块中:

#if DEBUG
    try
    {
        //...
    }
    finally
    {
        Console.WriteLine("Press enter to close...");
        Console.ReadLine();
    }
#endif

回答by zellio

The program is closing as soon as it's execution is complete. In this case when you return 0;. This is expected functionality. If you want to see the output then either run it in a terminal manually or set a wait at the end of the program so that it will stay open for a few seconds ( using the threading library ).

程序在执行完成后立即关闭。在这种情况下,当您return 0;. 这是预期的功能。如果您想查看输出,那么要么在终端中手动运行它,要么在程序结束时设置等待,使其保持打开状态几秒钟(使用线程库)。

回答by Robert Rouhani

The program immediately closes because there's nothing stopping it from closing. Insert a breakpoint at return 0;or add Console.Read();before return 0;to prevent the program from closing.

程序立即关闭,因为没有什么可以阻止它关闭。插入断点 atreturn 0;或 add Console.Read();beforereturn 0;以防止程序关闭。

回答by Ari

The code is finished, to continue you need to add this:

代码已完成,要继续,您需要添加以下内容:

Console.ReadLine();

or

或者

Console.Read();

回答by John Woo

Add The Readmethod to show the output.

添加Read显示输出的方法。

Console.WriteLine("Hello, World!");
Console.Read();
return 0;

回答by Ravi Gadag

Use Console.Read(); to prevent the program from closing, but make sure you add the Console.Read();code before return statement, or else it will be a unreachable code .

使用 Console.Read(); 以防止程序关闭,但请确保Console.Read();在 return 语句之前添加代码,否则它将是无法访问的代码。

    Console.Read(); 
    return 0; 

check this Console.Read

检查这个Console.Read

回答by Bhavik Patel

Instead of using

而不是使用

Console.Readline()
Console.Read()
Console.ReadKey()

you can run your program using Ctrl+F5(if you are in Visual Studio). Then Visual Studio will keep the console window open, until you press a key.

您可以使用Ctrl+运行您的程序F5(如果您在 Visual Studio 中)。然后 Visual Studio 将保持控制台窗口打开,直到您按下某个键。

Note: You cannot debug your code in this approach.

注意:您无法在此方法中调试代码。

回答by Marc

Add the following before the return 0:

在返回 0 之前添加以下内容:

system("PAUSE");  

This prints a line to hit a key to close the window. It will keep the window up until you hit the enter key. I have my students add it to all their programs.

这将打印一行以点击关闭窗口的键。它会一直保持窗口,直到您按下回车键。我让我的学生将它添加到他们所有的程序中。

回答by NagaHareeshChoudary

According to my concern, if we want to stable the OUTPUT OF CONSOLE APPLICATION, till the close of output display USE, the label: after the MainMethod, and goto label; before end of the program

根据我的担心,如果我们想稳定控制台应用程序的输出,直到输出显示USE,标签:在MainMethod之后,和goto标签;节目结束前

In the Program.

在程序中。

eg:

例如:

static void Main(string[] args)
{
    label:

    // Snippet of code

    goto label;
}

回答by gotqn

Alternatively, you can delay the closing using the following code:

或者,您可以使用以下代码延迟关闭:

System.Threading.Thread.Sleep(1000);

Note the Sleepis using milliseconds.

请注意Sleep使用毫秒。