398

My console applications on Visual Studio are closing automatically, so I'd like to use something like C's system("PAUSE") to "pause" the applications at the end of its execution, how can I achieve that?

Zignd
  • 6,246
  • 11
  • 34
  • 57

9 Answers9

606
Console.ReadLine();

or

Console.ReadKey();

ReadLine() waits for , ReadKey() waits for any key (except for modifier keys).

Edit: stole the key symbol from Darin.

Adam
  • 14,638
  • 2
  • 40
  • 63
  • 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
  • 16
    This 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
  • 2
    Silva'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
192

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.

Smi
  • 12,505
  • 9
  • 53
  • 61
Silvia Z
  • 2,002
  • 1
  • 10
  • 2
  • That'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
49

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.

Sohail xIN3N
  • 2,713
  • 1
  • 25
  • 29
40

Console.ReadLine() to wait for the user to Enter or Console.ReadKey to wait for any key.

Darin Dimitrov
  • 960,118
  • 257
  • 3,196
  • 2,876
26

Use:

Console.ReadKey();

For it to close when someone presses any key, or:

Console.ReadLine();

For when the user types something and presses enter.

matthewr
  • 4,409
  • 5
  • 26
  • 37
14

Alternatively, you can delay the closing using the following code:

System.Threading.Thread.Sleep(1000);

Note the Sleep is using milliseconds.

gotqn
  • 36,464
  • 39
  • 145
  • 218
14

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.

11

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.

Eric
  • 2,139
  • 2
  • 17
  • 19
4

If you do not want the program to close even if a user presses anykey;

 while (true) {
      System.Console.ReadKey();                
 };//This wont stop app
PodTech.io
  • 3,708
  • 32
  • 23