8

This is probably an easy Visual Studio question, but I couldn't find it on the site.

When I "Start Debugging" a console program, it immediately exits when its finished. Is there any way to have it pause when it ends without putting an explicit pause command at the end of your program?

Unknown
  • 45,913
  • 27
  • 138
  • 182
  • Hope you find the answer to this related question useful: http://stackoverflow.com/questions/193469/how-to-make-visual-studio-pause-after-executing-a-console-app-in-debug-mode – amit kumar Jun 30 '09 at 03:22

6 Answers6

10

Add a breakpoint just before the application finishes.

corné
  • 788
  • 7
  • 14
  • +1 If you dont want to make code changes + want to debug too, I cant think of anything else. – Gishu May 12 '09 at 07:39
  • 2
    What if there are multiple exit points? (not that my console program has any yet) – Unknown May 12 '09 at 07:39
  • well I gave you +1. Unfortunately this technique also hides the console window. – Unknown May 12 '09 at 07:42
  • 1
    @Unknown- There aren't many good reasons to have multiple exit points in a console program. You usually want to have 1 top-level error handler. – RossFabricant May 12 '09 at 08:13
  • Not always possible, I have cases where a breakpoint at the very end seems to be ignored (there was no return statement, for what matters). – gatopeich Mar 26 '12 at 13:21
6

You can place Console.ReadLine at the end of the program. That will force program to wait for a newline character input. Or you can place breakpoint at the end of the probram.

Vadym Stetsiak
  • 1,974
  • 18
  • 22
3

"Run without Debugging" does that, but I guess you want to debug still :)

leppie
  • 115,091
  • 17
  • 196
  • 297
1

Console.ReadKey() should do it. It will pause the execution of your program until a key is pressed on the keyboard.

Sonny Boy
  • 7,848
  • 18
  • 76
  • 104
  • Read again this part of the question "without putting an explicit pause command at the end of your program" :) – Burkhard May 12 '09 at 07:57
1

yes as @matthew said Console.ReadKey() will wait for your input after executing program, but you can use Console.ReadLine() which will terminate only if <ENTER> key is pressed:

void main()
{
    Console.WriteLine("Hello World!"); //:)
    Console.ReadLine();//this will do the trick.
}
Community
  • 1
  • 1
TheVillageIdiot
  • 40,053
  • 20
  • 133
  • 188
0

In visual studio 2019 you can disable closing automatically after finishing execution.

Debug(menue tab) -> options

Debugging -> general

uncheck "Automatically close the console when debugging stops"