Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does VS compile console applications to show "Press any key to continue"?

When I develop a C# console application (which will run on a server) and I run it using Visual Studio, I get a "Press any key to continue" message before the program terminates.

However, when I compile the very same C# code file manually using CSC, my program doesn't show that message and it terminates immediately after finishing its logic.

Does anyone know how I can make the same feature when compiling the code without using VS and WITHOUT changing the C# code any adding a ReadLine()?

UPDATE : The same message used to appear when I learned C#, I used to use TextPad with CSC, and that message used to appear without adding any Write(Line)/Read(Line) callings

like image 786
Moayad Mardini Avatar asked Jul 09 '09 12:07

Moayad Mardini


People also ask

How do I run a console application in Visual Studio?

Build and run your code in Visual Studio To run the code, on the menu bar, choose Debug, Start without debugging. A console window opens and then runs your app. When you start a console app in Visual Studio, it runs your code, then prints "Press any key to continue . . ." to give you a chance to see the output.

How do I keep the console window in Visual Studio?

Start the project with Ctrl + F5 instead of just F5 . The console window will now stay open with the Press any key to continue . . . message after the program exits.

What does a console application run through?

A console application is a program that requires no graphical user interface to run and can be completely run from a command line environment.

What function will be run automatically when a console program starts?

The Main function is a special function which is automatically called when a console application runs.


2 Answers

You could do this, if you want the same functionality when debugging.

if (Debugger.IsAttached)
{
    Console.WriteLine("Press any key to continue . . . ");
    Console.ReadKey(true);
}
like image 144
Siewers Avatar answered Oct 23 '22 13:10

Siewers


That's not possible. The prompt to press any key is generated by Visual Studio when running a console app. It's not part of your program.

The only way is by using Console.Read() in your code

UPDATE: concerning your remark on using TextPad: I'm not familiar with TextPad, but I wouldn't be surprised if TextPad did the same thing as Visual Studio when running a console app.

like image 40
Philippe Leybaert Avatar answered Oct 23 '22 12:10

Philippe Leybaert