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

前端 未结 21 2243
死守一世寂寞
死守一世寂寞 2020-11-22 03:45

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 i

相关标签:
21条回答
  • 2020-11-22 04:04

    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 ).

    0 讨论(0)
  • 2020-11-22 04:05

    Here is a way to do it without involving Console:

    var endlessTask = new TaskCompletionSource<bool>().Task;
    endlessTask.Wait();
    
    0 讨论(0)
  • 2020-11-22 04:06

    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;
    }
    
    0 讨论(0)
  • 2020-11-22 04:08

    if your program requires you to press enter to continue like you have to enter a value and continue, then add a new double or int and type write before retunr(0); scanf_s("%lf",&the variable);

    0 讨论(0)
  • 2020-11-22 04:10

    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
    
    0 讨论(0)
  • 2020-11-22 04:11

    I'm a little bit late to the party, but: in Visual Studio 2019 for .NET Core projects the console doesn't close automatically by default. You can configure the behaviour through menu Tools → Options → Debugging → General → Automatically close the console when debugging stops. If you get your console window automatically closing, check if the mentioned setting is not set.

    The same applies to the .NET Framework new style console projects:

    <Project Sdk="Microsoft.NET.Sdk">
    
      <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>net472</TargetFramework>
      </PropertyGroup>
    
    </Project>
    

    The old style .NET Framework project still unconditionally close the console at the end (as of Visual Studio 16.0.1).

    Reference: https://devblogs.microsoft.com/dotnet/net-core-tooling-update-for-visual-studio-2019-preview-2/

    0 讨论(0)
提交回复
热议问题