[聚合问答] Why is the console window closing immediately without displaying my output?

c#,.net,console-application 2018-01-23 33 阅读

I've decided to study some basic C# (though I have experience when it comes to programming) the thing is I am following the guides in MSDN, but the issue here is that their Hello World Program is showing up then it would immediately close. why is that?

using System;

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

10个回答

194

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

2018-01-23
46

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.

Note: You cannot debug your code in this approach.

2018-01-23
8

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();
  }
}

2018-01-23
6

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.

2018-01-23
4

Another way is to use Debugger.Break() before returning from Main method

2018-01-23
3

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

Console.ReadLine();

or

Console.Read();

2018-01-23
3

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(); 
    return 0; 

check this Console.Read

2018-01-23
3

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

System.Threading.Thread.Sleep(1000);

Note the Sleep is using milliseconds.

2018-01-23
3

Add The Read method to show the output.

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

2018-01-23
3

I assume the reason you don't want it to close in Debug mode, is because you want to look at the values of variables etc. So it's probably best to just insert a break-point on the closing "}" of the main function. If you don't need to debug, then Ctrl-F5 is the best option.

2018-01-23

注:本文内容来自互联网,旨在为开发者提供分享、交流的平台。如有涉及文章版本等事宜,请你联系站长进行处理。