Visual Studio terminates my console application too fast

前端 未结 5 681
温柔的废话
温柔的废话 2020-12-21 19:12

When I execute my program in Visual Studio (just a simple hello world app) it terminates and closes the console window immediately, rather than waiting for me to close it ma

相关标签:
5条回答
  • 2020-12-21 19:53

    This will work for sure: In old Visual studio 2010 and older, system properties were grandfathered from windows system, however 2010 and up this property was customized or set ti null. do the following and this will fix it for sure.

    To do this select the project in the solution explorer on the right or left (probably is already selected so you don't have to worry about this). Then select "project" from the menu bar drop down menus, then select "*project_name* properties" > "configuration properties" > "linker" > "system" and set the first property, the drop down "subsystem" property to "console (/SUBSYSTEM:CONSOLE)". The console window should now stay open after execution as usual.

    You are good to go NOW!!

    0 讨论(0)
  • 2020-12-21 19:56

    While testing Console.ReadKey() is nice to have. Here's an example:

    namespace Foo
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.Write("The Console will close when it reads a key-input. "+
                              "Press a key to close");
                Console.ReadKey();
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-21 20:10

    This is by design and your instructor is incorrect. Try launching a .bat file from a folder view and you get precisely the same behaviour!

    You can

    • Set a breakpoint

      Ask for user input via Console.Readline()

      Run to cursor

    0 讨论(0)
  • 2020-12-21 20:13

    You can set a breakpoint and start the application in Debug mode. This way the IDE will halt at the breakpoint and the window doesn't get closed until you continue the execution of your code.

    0 讨论(0)
  • 2020-12-21 20:16

    Start in debug mode ;)

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