51

I'm using Visual Studio 2010 and Windows 7 x64

The command prompt closes after exit, even though I used "Start without debug". Is there a setting somewhere that I can use?

segfault
  • 5,759
  • 9
  • 45
  • 66

5 Answers5

102

You can simply press Ctrl+F5 instead of F5 to run the built code. Then it will prompt you to press any key to continue. Or you can use this line -> system("pause"); at the end of the code to make it wait until you press any key.

However, if you use the above line, system("pause"); and press Ctrl+F5 to run, it will prompt you twice!

rink.attendant.6
  • 44,500
  • 61
  • 101
  • 156
Suhas Bharadwaj
  • 1,345
  • 1
  • 13
  • 10
  • 2
    This should be the most-voted answer. – Apache Jan 22 '13 at 16:42
  • agreed. I've marked this as the correct answer. – segfault Jan 23 '13 at 20:29
  • 6
    I don't understand why the question says the console closes even though he runs with "Start without debug", but Ctrl+F5 works, which is simply a hot-key for "Start without debug". – jfritz42 Apr 19 '13 at 22:24
  • Running ctrl + F5 will run the application without debugger. (http://msdn.microsoft.com/en-US/library/8b59xk0f(v=vs.90).aspx) This is not the same as running in release mode. – Markus Sep 19 '13 at 17:37
  • If I press ctrl+f5 (or choose 'start without debugging') nothing happens at all. The application isn't even started... – Kokodoko Mar 29 '15 at 11:00
  • @Kokodoko did you build your project before you attempted to execute it? – scottysseus May 23 '15 at 15:06
  • I still does not understand why it is not possible to run in debug mode and have that same behavior. it seems weird we need to alter the code with a readline or adding a break point to have it stop. :-( – Alberto Sep 22 '16 at 15:09
  • 1
    I just can't believe it is considered a solution. IDE should be configured to behave as we want without forcing us to change our code based on IDE behavior. I'm deeply committed with my code so it is always ready to release. – MFedatto Nov 18 '20 at 15:07
45

Yes, in VS2010 they changed this behavior somewhy.
Open your project and navigate to the following menu: Project -> YourProjectName Properties -> Configuration Properties -> Linker -> System. There in the field SubSystem use the drop-down to select Console (/SUBSYSTEM:CONSOLE) and apply the change.
"Start without debugging" should do the right thing now.

Or, if you write in C++ or in C, put

system("pause");

at the end of your program, then you'll get "Press any key to continue..." even when running in debug mode.

cmsjr
  • 56,771
  • 11
  • 70
  • 62
lapis
  • 6,872
  • 4
  • 35
  • 40
8

What about Console.Readline();?

rink.attendant.6
  • 44,500
  • 61
  • 101
  • 156
Rahul Soni
  • 4,941
  • 3
  • 35
  • 58
6

Add a Console.ReadKey call to your program to force it to wait for you to press a key before exiting.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
2

You could open a command prompt, CD to the Debug or Release folder, and type the name of your exe. When I suggest this to people they think it is a lot of work, but here are the bare minimum clicks and keystrokes for this:

  • in Visual Studio, right click your project in Solution Explorer or the tab with the file name if you have a file in the solution open, and choose Open Containing Folder or Open in Windows Explorer
  • in the resulting Windows Explorer window, double-click your way to the folder with the exe
  • Shift-right-click in the background of the explorer window and choose Open Commmand Window here
  • type the first letter of your executable and press tab until the full name appears
  • press enter

I think that's 14 keystrokes and clicks (counting shift-right-click as two for example) which really isn't much. Once you have the command prompt, of course, running it again is just up-arrow, enter.

Kate Gregory
  • 18,808
  • 8
  • 56
  • 85
  • The OP wanted the console to stay open when debugging in Visual Studio - this answer does not help with that. This is more if you wanted to just run it without debugging. Also, if you're interested in what's too many keystrokes and what's not too many, please have a look at Nir Eyal's book on user interfaces. Turns out, 14 is a lot of steps. – RhetoricalRuvim Aug 01 '19 at 18:24