My console applications on Visual Studio are closing automatically once the program finishes the execution. I'd like to "pause" the applications at the end of their execution so that I can easily check the output. How can I achieve that?
9 Answers
Update:
As of Visual Studio 2017, there's a built-in option in the IDE at:
Tools > Options > Debugging > Automatically close the console when debugging stops
Unchecking it will automatically pause the application at the end of the execution, allowing you the easily check its output without having to add extra code to your application.
There are two ways;
Console.ReadLine();
ReadLine()
waits for ↩
or
Console.ReadKey();
ReadKey()
waits for any key (except for modifier keys).
-
2"'Console' does not contain a definition for 'ReadKey' in asp.net 5 console App" comes up, go here: http://stackoverflow.com/questions/30588478/console-does-not-contain-a-definition-for-readkey-in-asp-net-5-console-app – Paul Totzke Nov 30 '15 at 17:16
-
19This is actually a bad answer because it gives the OP what they asked for rather than what they need. Telling the OP to break his software to work around the way he's launching it is just bad advice. Among other things, it makes it impossible to use the program in a pipeline. – David Schwartz Jan 14 '16 at 23:47
-
2Silva's answer is better, to run with Control-F5 – Del Aug 04 '16 at 14:19
-
This also redirects output though. So if you have a timer running or another thread that logs to the console, you wont see it at all. (This might only be the case for Console.Read and not the other methods). – KthProg Mar 15 '17 at 13:50
-
My console application require admin privilege. So, CTRL+F5 will be ask for UAC and the code run and exit immediately. @Adam's answer is the best for this. – vee Jan 21 '19 at 05:11
-
Console.ReadLine(), for when only "Press any Enter Key..." will do. ;-P Gets my upvote anway, though. – Robin Davies Apr 12 '20 at 17:57
-
This is a good answer because it answers the question, which is the first thing that shows up in DuckDuckGo – Thomas Harris Jul 01 '20 at 12:16
You can just compile (start debugging) your work with Ctrl+F5.
Try it. I always do it and the console shows me my results open on it. No additional code is needed.
-
3That's not useful to people who are running the console program outside of visual studio though. i.e.: double-clicking on the .exe – NickLokarno Jan 26 '21 at 16:11
-
Try Ctrl + F5 in Visual Studio to run your program, this will add a pause with "Press any key to continue..." automatically without any Console.Readline() or ReadKey() functions.

- 2,951
- 2
- 30
- 29
Console.ReadLine()
to wait for the user to Enter or Console.ReadKey
to wait for any key.

- 1,023,142
- 271
- 3,287
- 2,928
Use:
Console.ReadKey();
For it to close when someone presses any key, or:
Console.ReadLine();
For when the user types something and presses enter.

- 4,679
- 5
- 30
- 40
Alternatively, you can delay the closing using the following code:
System.Threading.Thread.Sleep(1000);
Note the Sleep
is using milliseconds.

- 42,737
- 46
- 157
- 243
-
16Thread.Sleep(TimeSpan.FromSeconds(1)) if anyone wanted to mentally work with seconds – Frison Alexander Sep 22 '16 at 17:06
-
13
Ctrl + F5 is better, because you don't need additional lines. And you can, in the end, hit enter and exit running mode.
But, when you start a program with F5 and put a break-point, you can debug your application and that gives you other advantages.

- 183
- 1
- 5
Those solutions mentioned change how your program work.
You can off course put #if DEBUG
and #endif
around the Console calls, but if you really want to prevent the window from closing only on your dev machine under Visual Studio or if VS isn't running only if you explicitly configure it, and you don't want the annoying 'Press any key to exit...'
when running from the command line, the way to go is to use the System.Diagnostics.Debugger
API's.
If you only want that to work in DEBUG
, simply wrap this code in a [Conditional("DEBUG")] void BreakConditional()
method.
// Test some configuration option or another
bool launch;
var env = Environment.GetEnvironmentVariable("LAUNCH_DEBUGGER_IF_NOT_ATTACHED");
if (!bool.TryParse(env, out launch))
launch = false;
// Break either if a debugger is already attached, or if configured to launch
if (launch || Debugger.IsAttached) {
if (Debugger.IsAttached || Debugger.Launch())
Debugger.Break();
}
This also works to debug programs that need elevated privileges, or that need to be able to elevate themselves.

- 2,797
- 2
- 20
- 19
If you do not want the program to close even if a user presses anykey;
while (true) {
System.Console.ReadKey();
};//This wont stop app

- 4,874
- 41
- 24
-
10
-
3This will create 100% CPU usage if you run the app as a service. – Sean Thorburn Mar 20 '21 at 17:58