Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Press any key to continue --- console application

If i press any key and the console is closed , why is it "press any key to continue" and not "press any key to exit" ?

(https://i.sstatic.net/lUcrC.jpg)

like image 597
Eli Yakubov Avatar asked Oct 27 '25 17:10

Eli Yakubov


1 Answers

This is probably a question best answered by whomever wrote the console app you're using.

That being said. Broadly speaking, when you see that message the console is waiting for input, a key stroke. Typically at some code that looks like:

Console.WriteLine("Press any key to continue...");
Console.ReadKey();

Once you hit that key you are not explicitly exiting the console. You are just letting the program resume execution again. Typically something like this will be at the end of the program so resuming execution will finish the program but not always. As console programs are typically ran from a command line once the program has finished the console window itself still remains. So, even though the program may have finished the command window is still going to be open waiting for the next command. In the case of launching it from Visual Studio the console app wasn't launched from a command window it was launched directly* and thus there's no other UI to stay open. Of course that might be splitting hairs.

But, in a very technical sense the program isn't exiting it's just being allowed to continue to run till it does finish/exit.

  • In VS2019 by default both C++ and managed console apps no longer close the window when they exit. In VS 2019 the window stays up as the console apps are launched from another host console.
like image 122
Andy Sterland Avatar answered Oct 29 '25 06:10

Andy Sterland



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!