Question:
How do I stop a C# console app from closing?
Aytches
2011-08-23 22:07:34 UTC
It's been awhile, I thought there was a simple command like "pause" or something that just stopped the console from closing.

I know I can add a Console.Read() but I just thought there was a different way, specifically to stop closing the console.
Three answers:
mdigitale
2011-08-23 22:15:58 UTC
Just set a break point at the end of your main function.
sporkatron
2011-08-23 22:13:50 UTC
You can use a do while loop to wait for the user to enter some specific input such as '0' or 'Q' or whatever you want to quit.



bool exit=false;

do{

Console.WriteLine("Enter Q to quit");

string s = Console.ReadLine();

if(s=="Q")

{ exit = true;}

}(while !exit)





As far as 'pause' goes there is a wait function I think, but it is something that you really aren't supposed to use. Can't remember exactly why, I think it was more of a system resource thing. Busy waiting like above may tie up the CPU a little more, but its the better way to go.





Oh and also, if you compile your program and then open up a console window from your start bar, navigate to the directory your file is and run it from there, instead of running it in Visual Studio debug the window won't close on you.
hindelang
2016-12-13 13:14:16 UTC
confident, this question is asked many circumstances. word that the habit you're encountering is thoroughly widely used. in case you visit the domicile windows Explorer and execute a console application from there, then maximum courses will execute and then close at present day. What I do is to place a breakpoint on the tip of the considerable function; I positioned it on the "}" (the curly bracket) on the tip of the considerable function. in case you do this then you definately might ought to click on the utility interior the taskbar to determine it. word that in case you execute using F5 then seen Studio will use the debugger to execute this technique and the console window would be closed on the tip of execution. in case you do no longer ought to debug this technique, then use Ctrl-F5 and then this technique would be accomplished without the debugger and the console window will stay open after this technique has accomplished.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...