views:

1585

answers:

6

Hi,

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

+11  A: 

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.

Philippe Leybaert
you can add the line Console.Read() and comment it out when delivering. However the best approach I know is to use logging and log all info you need at lower level such as Info or Trace or Debug and desable it when shipping :-)
Ratnesh Maurya
Please see my update above.
Moayad Mardini
OK, thanks. Regarding your update, as I mentioned in my comment on Andrew's answer, TextPad generates batch files automatically to accomplish that.
Moayad Mardini
The prompt is not generated by Visual Studio, but by cmd.exe's pause command
Pete Kirkham
+4  A: 

You could write a batch script that runs the exe for you and prompts the user to press a key.

The batch script would look something like this:

echo off
YourApp.exe
pause
Andrew Hare
Thanks, TextPad does generate batch files automatically.
Moayad Mardini
I'd suggest Chalkey's answer instead.
Steven Sudit
+2  A: 

This behavior has nothing to do with the compiler you are using. When you compile with Visual Studio, running the executable outside of Visual Studio actually will perform exactly the same as when you compile with CSC on the command line. Visual Studio (and TextPad) is adding the logic to add the "Press any key to continue" message on the console.

If you want your application to stay open, you will need to do something like Console.ReadLine() to block execution so that your application does not complete its execution.

heavyd
+8  A: 

It's nothing to do with the compiler - if you press F5 to debug it rather than Ctrl-F5 to run without debugging, then VS doesn't show the prompt. This is presumably so that you don't miss whatever output it's producing.

In order to do this, Visual Studio runs cmd.exe telling it to run your executable and then pause:

"C:\WINDOWS\system32\cmd.exe" /c ""...\ConsoleApplication1.exe"  & pause"

It probably doesn't do it when you debug as it's a bit harder to get the process ID of a child of a child process.

To add a similar option to your program, either use a command line switch to tell the application itself to pause, or use a batch file to run it then pause, or use a shortcut with them cmd.exe /c.

Pete Kirkham
+7  A: 

You could do this...

static void Main(string[] args)
{
#if DEBUG
    Console.Read();
#endif
}

That way the program will not wait for the console input when you build your application as a 'Release'.

Chalkey
+1  A: 

The question is why would you want to have this behaviour? The Press any key to continue feature is there so that you can see the output of your application. If on the other hand you build your code and run it from a command prompt (console), this will not close when the application finishes, so you can see the output.

As noted above, the Press any key to continue is a feature of the IDE and not related to the code you are writing. The purpose of that feature is to allow you to see the output of you console application.

Salo