Eli Yakubov
Eli Yakubov

Reputation: 41

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)

Upvotes: 4

Views: 12676

Answers (1)

Andy Sterland
Andy Sterland

Reputation: 1936

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.

Upvotes: 6

Related Questions