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

C# 为什么控制台窗口在显示我的输出后立即关闭?,c#,.net,console-application,msdn,C#,.net,Console Application,Msdn,我正在按照MSDN中的指南学习C# 现在,我刚刚尝试了示例1(是指向MSDN的链接),我遇到了一个问题:为什么控制台窗口在显示我的输出后立即关闭 using System; public class Hello1 { public static int Main() { Console.WriteLine("Hello, World!"); return 0; } } 程序在执行完成后立即关闭。在这种情况下,返回0。这是预期的功能。如果

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

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

using System;

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

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

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

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

因为它已完成。当控制台应用程序已完成执行并从其
main
方法返回时,关联的控制台窗口将自动关闭。这是预期的行为

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

这是一种方法。将此行添加到代码末尾(就在
return
语句之前)将导致应用程序在退出之前等待您按键

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

最好的折衷办法可能是仅在通过将应用程序包装在预处理器指令中进行调试时调用
Console.ReadLine
方法。比如:

#if DEBUG
    Console.WriteLine("Press enter to close...");
    Console.ReadLine();
#endif
如果引发了未捕获的异常,您可能还希望窗口保持打开状态。为此,您可以放置
控制台.ReadLine()
最后
块中:

#if DEBUG
    try
    {
        //...
    }
    finally
    {
        Console.WriteLine("Press enter to close...");
        Console.ReadLine();
    }
#endif
这里的问题是,他们的Hello World程序出现了,然后它将立即关闭。
为什么呢

因为它已完成。当控制台应用程序已完成执行并从其
main
方法返回时,关联的控制台窗口将自动关闭。这是预期的行为

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

这是一种方法。将此行添加到代码末尾(就在
return
语句之前)将导致应用程序在退出之前等待您按键

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

最好的折衷办法可能是仅在通过将应用程序包装在预处理器指令中进行调试时调用
Console.ReadLine
方法。比如:

#if DEBUG
    Console.WriteLine("Press enter to close...");
    Console.ReadLine();
#endif
如果引发了未捕获的异常,您可能还希望窗口保持打开状态。为此,您可以放置
控制台.ReadLine()
最后
块中:

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

程序立即关闭,因为没有任何东西可以阻止它关闭。在
返回0处插入断点
或添加
Console.Read()
before
返回0以防止程序关闭。

程序立即关闭,因为没有任何东西阻止它关闭。在
返回0处插入断点
或添加
Console.Read()
before
返回0以防止程序关闭。

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

Console.ReadLine();


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

Console.ReadLine();


添加
Read
方法以显示输出

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

添加
Read
方法以显示输出

Console.WriteLine("Hello, World!");
Console.Read();
return 0;
使用Console.Read();防止程序关闭,但请确保添加
Console.Read()
code before return语句,否则它将是无法访问的代码

    Console.Read(); 
    return 0; 
选中此项

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

    Console.Read(); 
    return 0; 
检查此项,而不是使用

Console.Readline()
Console.Read()
Console.ReadKey()
您可以使用Ctrl+F5运行程序(如果您在Visual Studio中)。然后VisualStudio将保持console窗口打开,直到您按键

注意:您不能用这种方法调试代码。

而不是使用

Console.Readline()
Console.Read()
Console.ReadKey()
您可以使用Ctrl+F5运行程序(如果您在Visual Studio中)。然后VisualStudio将保持console窗口打开,直到您按键


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

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

system("PAUSE");  

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

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

system("PAUSE");  

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

根据我的担心,如果我们想要稳定控制台应用程序的输出,直到输出显示使用结束,标签:在main方法之后,并转到标签;节目结束前
SpinWait.SpinUntil(() => false);
[DebuggerHidden, DebuggerStepperBoundary, DebuggerNonUserCode, DllImport("user32.dll", EntryPoint = "PeekMessage")]
public static extern int PeekMessage(out NativeMessage lpMsg, IntPtr hWnd, int wMsgFilterMin, int wMsgFilterMax, int wRemoveMsg);

[DebuggerHidden, DebuggerStepperBoundary, DebuggerNonUserCode, DllImport("user32.dll", EntryPoint = "GetMessage")]
public static extern int GetMessage(out NativeMessage lpMsg, IntPtr hWnd, int wMsgFilterMin, int wMsgFilterMax);

[DebuggerHidden, DebuggerStepperBoundary, DebuggerNonUserCode, DllImport("user32.dll", EntryPoint = "TranslateMessage")]
public static extern int TranslateMessage(ref NativeMessage lpMsg);

[DebuggerHidden, DebuggerStepperBoundary, DebuggerNonUserCode, DllImport("user32.dll", EntryPoint = "DispatchMessage")]
public static extern int DispatchMessage(ref NativeMessage lpMsg);

[DebuggerHidden, DebuggerStepperBoundary, DebuggerNonUserCode]
public static bool ProcessMessageOnce()
{
    NativeMessage message = new NativeMessage();

    if (!IsMessagePending(out message))
        return true;

    if (GetMessage(out message, IntPtr.Zero, 0, 0) == -1)
        return true;

    Message frameworkMessage = new Message()
    {
        HWnd = message.handle,
        LParam = message.lParam,
        WParam = message.wParam,
        Msg = (int)message.msg
    };

    if (Application.FilterMessage(ref frameworkMessage))
        return true;

    TranslateMessage(ref message);
    DispatchMessage(ref message);

    return false;
}
while (true)
    ProcessMessageOnce();
var endlessTask = new TaskCompletionSource<bool>().Task;
endlessTask.Wait();
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net472</TargetFramework>
  </PropertyGroup>

</Project>