views:

660

answers:

5

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?

+2  A: 

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

leppie
Ah this is what I wanted. Its a pity that you can't debug with that. I'll accept your answer unless a better one comes up.
Unknown
This does not work if you are starting an external program rather than the project ...
Jeffrey Cameron
+1  A: 

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

Sonny Boy
Read again this part of the question "without putting an explicit pause command at the end of your program" :)
Burkhard
+3  A: 

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.

Vadmyst
+4  A: 

Add a breakpoint just before the application finishes.

corné
+1 If you dont want to make code changes + want to debug too, I cant think of anything else.
Gishu
What if there are multiple exit points? (not that my console program has any yet)
Unknown
Then add multiple breakpoints :)
corné
well I gave you +1. Unfortunately this technique also hides the console window.
Unknown
@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
+1  A: 

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.
}
TheVillageIdiot