In this article, I am sharing small but important information that I encountered during my early days of programming.
Problem Statement
As I said in the introduction, during my initial days of programming I encountered an issue. I was developing a small application, where I was supposed to display multiple messages on the console window and once the last message is displayed, after a wait of 5 seconds the console window was supposed to close automatically. That seems like a straight forward requirement, right? Here is the code for this:
- static void Main(string[] args)
- {
- Console.WriteLine("Message1");
- Console.WriteLine("Message2");
- ..and so on
- Console.WriteLine("This is the last message.");
- Thread.Sleep(5000); //Delay for 5 seconds
- //Close Window, but How?
- //return 0;
- //System.Environment.Exit(1);
- }
Analysis [Resolution]
The reason why I have titled this section as “Analysis” is because this is not a resolution but just an answer to this misconception with respect to the problem.
Press any key to continue is by default provided by Microsoft in the development IDE. Whenever we run an application from the IDE, we expect any output, right? And we need that console window to remain open so that we can see the output. This is given for that purpose so that the user can see the output generated. No matter whether our project is running in Debug or Release mode we will get this message. If we go through any MSDN documentation, they specifically mention in the code sample:
- // Keep the console open in debug mode.
- Console.WriteLine("Press any key to exit.");
- Console.ReadKey();
If we run this from outside the IDE, in other words directly by double-clicking the .exe file generated, we will not find this message, also in real-time we run the .exe file from a BAT file or something (obviously we will not ask the end user to run our app from Visual Studio IDE) then too we won't get this message.
Now, simply run the program by directly double-clicking the .exe file present in the bin folder of solution, we will see the desired output, in other words after 5 seconds the console window “automatically” closes.
Thus, in short this message is only specific to the IDE for development purposes.

Guest UserPosted Dec 17, 2014, 7:46 AM
Good discussion guys! How about using compiler instructions to control things at debug time, e.g., #if debug
Sam HobbsPosted Dec 16, 2014, 4:49 PM
Console windows have always worked like that. Even before console windows, there were DOS programs and the Microsoft C IDE under 16-bit Windows and DOS worked like that. Here, the ReadKey is only needed when the program is executed using the debugger (in VS), correct? And yet in production users also need to press a key, correct? My suggestion is to simply put a breakpoint at the last line of the console program (the bottom of Main). Then the breakpoint provides a pause only when debugging.
Vithal WadjePosted Dec 16, 2014, 7:29 AM
nice but should be something different ,people knows windows automatically closes when user press Eneter button or using console.exit ..articcle is related to the threading ,so can also say Close window with Predefined time