Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

29/04/2015 How to stop C# console applications of closing automatically?

 ­ Stack Overflow
 

 
 
 
 
sign up log in tour help  stack overflow careers

Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no
Take the 2­minute tour   ×
registration required.

How to stop C# console applications of closing automatically?

I just started reading C# How to Program 2010, and my console applications are closing automatically, so, I would like to use something like
System("PAUSE")  on C to stop the applications in the end, but what is the code that should I use?

c#   console­application

asked Jul 16 '12 at 21:26
Zignd
1,047 3 13 35

10   I like to see such a good,specific, clearly written beginner question! +1. –  David Stratton Jul 16 '12 at
21:28

    And you did a great job. It was a compliment. ;­) –  David Stratton Jul 16 '12 at 21:43

    possible duplicate of Why is the console window closing immediately without displaying my output? – 
Carl Walsh Jan 14 at 18:00

7 Answers

Console.ReadLine();

or

Console.ReadKey();

http://stackoverflow.com/questions/11512821/how­to­stop­c­sharp­console­applications­of­closing­automatically 1/4
29/04/2015 How to stop C# console applications of closing automatically? ­ Stack Overflow

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

Edit: stole the key symbol from Darin.

edited Dec 26 '13 at 23:09 answered Jul 16 '12 at 21:27
codesparkle
7,038 1 18 38

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.

edited Oct 14 '13 at 18:39 answered Sep 13 '13 at 19:52
Smi Silvia Z
5,388 5 24 45 331 3 2

    why was this down voted? –  Dimitri Adamou Sep 24 '13 at 1:53

4   why was this UP voted? Obviously the OP isn't getting this behavior (he says his console app is closing
automatically). Indeed, a console  app does close automatically, unless something is done to keep it open
(such as waiting for user input). –  ToolmakerSteve Apr 27 '14 at 18:07 

1   Ctrl+F5 rebuilds the application (if necessary) and runs it without debugging (i.e. the debugger is not
attached) in a new console window where, after your application has exited, a  pause  command or similar
is run in the same window. The  pause  serves to give you a chance to read the output before the console
window closes. When the application is used from the command line no pause is needed, as the console
window does not close after a command has finished. –  Jeppe Stig Nielsen Aug 5 '14 at 21:36

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


for any key.

answered Jul 16 '12 at 21:27

http://stackoverflow.com/questions/11512821/how­to­stop­c­sharp­console­applications­of­closing­automatically 2/4
29/04/2015 How to stop C# console applications of closing automatically? ­ Stack Overflow
Darin Dimitrov
585k 92 1966 2017

Use:

Console.ReadKey();

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

Console.ReadLine();

For when the user types something and presses enter.

answered Jul 16 '12 at 21:27
MatthewRz
1,957 1 11 28

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.

answered Aug 5 '14 at 15:00
Sohail xIN3N
695 6 16

Use   if you don't want to show a key on console, you can use 
Console.ReadKey();
Console.Write("\b \b");  code after readkey. This will remove written key.

answered Aug 27 '13 at 12:25
viron
39 2

3   Use  Console.ReadKey(true)  if you don't wish the entered key to be visible on the console. –  Lukazoid

http://stackoverflow.com/questions/11512821/how­to­stop­c­sharp­console­applications­of­closing­automatically 3/4
29/04/2015 How to stop C# console applications of closing automatically? ­ Stack Overflow
Jan 12 at 16:54

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

System.Threading.Thread.Sleep(1000);

Note the  Sleep  is using milliseconds.

answered Dec 7 '14 at 12:40
gotqn
5,959 11 57 104

protected by Community ♦ Dec 4 '13 at 5:26
Thank you for your interest in this question. Because it has attracted low­quality answers, posting an answer now requires 10 reputation on this site. 

Would you like to answer one of these unanswered questions  instead?

http://stackoverflow.com/questions/11512821/how­to­stop­c­sharp­console­applications­of­closing­automatically 4/4

You might also like