RBT
RBT

Reputation: 25945

"Press any key to continue..." message shown in "Start Without Debugging" mode

I created a blank C# console application in Visual Studio as shown below:

using System;

namespace ConsoleApplication1
{
    class Solution
    {
        static void Main(string[] args)
        {
            Console.ReadLine();
        }
    }
}

When I use the default Start Debugging option by pressing F5 then the program runs normally as expected. I press Enter and program ends.

But when I use Start Without Debugging option by pressing Ctrl+F5 it shows me an extra message on console after I press Enter:

Press any key to continue...

enter image description here

Then I've to press an additional key on the keyboard to terminate the program. From where is this magical message coming and why it is shown only in Start Without Debugging option?

Note:Post-build event command line of the project is completely empty.

Upvotes: 3

Views: 4691

Answers (2)

RBT
RBT

Reputation: 25945

I was able to validate the information shared by @SoronelHaetir in his answer. I'm detailing out the same here with the help of some screenshots which will complement the information in his post and help you understand the same better:

  1. When I run the application using the default Start Debugging option by pressing F5, we can see the executable of the application getting launched in task manager:

enter image description here

When I right click on the task and choose Go To Process option from context menu, I'm taken to a process on the process tab having image name ConsoleApp1.exe *32 as shown below. This makes perfect sense.

enter image description here

  1. Now when I run the application using Start Without Debugging option by pressing Ctrl + F5, we do not see the executable of the application getting launched in task manager. In fact we see cmd.exe being launched as shown below:

enter image description here

Now, when I right click on the cmd.exe task and choose Go To Process option from context menu, I'm taken to a process on the process tab having an image name cmd.exe *32 as shown below. But there is more to it. You also see ConsoleApp1.exe *32 running in the process tab which was not visible in Applications tab.

enter image description here

So this is how all the dots get connected that in Start Without Debugging mode Visual Studio in fact launches a cmd.exe instance which in turn launches our application ConsoleApp1.exe.

The moment I press enter, ConsoleApp1.exe process gets terminated but cmd.exe process continue to live on until I press another key as shown below:

enter image description here

Upvotes: 0

SoronelHaetir
SoronelHaetir

Reputation: 15172

That is simply how visual studio runs console programs not in debug mode. As far as I am aware it can not be controlled. Since it shows that it is actually a cmd.exe instance and not just a console window I assume VS uses the /K flag on the command line (I had thought it used a batch file but now see there is no need for that).

It is done for the typical case where a console program runs and simply exits, without that message such a program would not give any chance to see the output.

Upvotes: 5

Related Questions