Why is the console window closing immediately once

2018-12-31 15:04发布

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

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

using System;

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

19条回答
怪性笑人.
2楼-- · 2018-12-31 15:23

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

Console.ReadLine();

or

Console.Read();
查看更多
闭嘴吧你
3楼-- · 2018-12-31 15:24

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

System.Threading.Thread.Sleep(1000);

Note the Sleep is using milliseconds.

查看更多
旧时光的记忆
4楼-- · 2018-12-31 15:25

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

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

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.ReadLine method is one way of doing that. Adding this line to the end of your code (just before the return statement) will cause the application to wait for you to press a key before exiting.

Alternatively, you could start the application without the debugger attached by pressing Ctrl+F5 from 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.

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

#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 finally block:

#if DEBUG
    try
    {
        //...
    }
    finally
    {
        Console.WriteLine("Press enter to close...");
        Console.ReadLine();
    }
#endif
查看更多
谁念西风独自凉
5楼-- · 2018-12-31 15:26

This behaves the same for CtrlF5 or F5. Place immediately before end of Main method.

using System.Diagnostics;

private static void Main(string[] args) {

  DoWork();

  if (Debugger.IsAttached) {
    Console.WriteLine("Press any key to continue . . .");
    Console.ReadLine();
  }
}
查看更多
余生无你
6楼-- · 2018-12-31 15:26

You can solve it very simple way just invoking the input. However, if you press Enter then the console will disapper again. Simply use this Console.ReadLine(); or Console.Read();

查看更多
春风洒进眼中
7楼-- · 2018-12-31 15:26

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

In the Program.

eg:

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

    // Snippet of code

    goto label;
}
查看更多
登录 后发表回答