Reputation: 3101
I have the following code in a C# console application:
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello...");
}
}
When I run this application, the command prompt appears and suddenly disappear, but I have seen at my friend's house when I run the application, the command prompt asks to Press any key to Continue
. When I press any key after then the application terminates.., but in my PC it doesn't work without writing Consol.ReadLine()
.
Is there a setting to get the user to the Press Any Key to Continue
line?
Upvotes: 2
Views: 178
Reputation: 111940
Sadly no, but if you Debug->Start Without Debugging Ctrl+F5 it will make that effect.
Clearly you could add
Console.Write("\nPress Any Key to Continue");
Console.Readkey();
at the end of your program.
If you want to be (nearly) sure that your code will always show that prompt:
AppDomain.CurrentDomain.ProcessExit += (sender, e) =>
{
Console.Write("\nPress Any Key to Continue");
Console.ReadKey();
};
Put these lines at the beginning of your Main()
method.
But I think this is a little overboard :-) It installs an exit handler that will ask for a key on exit.
Now, if you want to really really go overboard and show, you can go up to eleven:
if (Debugger.IsAttached)
{
AppDomain.CurrentDomain.ProcessExit += (sender, e) =>
{
Console.Write("\nPress Any Key to Continue");
Console.ReadKey();
};
}
This will ask for the key only if the debugger is attached (even these lines have to be put at the beginning of the Main()
method. They replace the other versions). With Start Without Debugging
the debugger isn't attached, so Visual Studio will show its prompt instead of yours.
(Instead of using the AppDomain.CurrentDomain.ProcessExit
, you could have protected the whole Main() with a try...finally
block, and in the finally put the Console.*
, but that wasn't funny :-) )
Upvotes: 5
Reputation: 4733
Disappearing is the correct behaviour. If you want it to appear this way just add
Console.ReadKey();
Of course you can add some message before ("Press any key...") etc.
Upvotes: 0