How to stop C# console applications from closing automatically?

C#Console Application

C# Problem Overview


My console applications on Visual Studio are closing automatically, therefore, I'd like to use something like C's system("PAUSE") to "pause" the applications at the end of their execution. How can I achieve that?

C# Solutions


Solution 1 - C#

There are two ways;

Console.ReadLine();

ReadLine() waits for

or

Console.ReadKey();

ReadKey() waits for any key (except for modifier keys).

Solution 2 - C#

You can just compile (start debugging) your work with Ctrl+F5.

Try it. I always do it and the console shows me my results open on it. No additional code is needed.

Solution 3 - C#

Try Ctrl + F5 in Visual Studio to run your program, this will add a pause with "Press any key to continue..." automatically without any Console.Readline() or ReadKey() functions.

Solution 4 - C#

Console.ReadLine() to wait for the user to Enter or Console.ReadKey to wait for any key.

Solution 5 - C#

Use:

Console.ReadKey();

For it to close when someone presses any key, or:

Console.ReadLine();

For when the user types something and presses enter.

Solution 6 - C#

Alternatively, you can delay the closing using the following code:

System.Threading.Thread.Sleep(1000);

Note the Sleep is using milliseconds.

Solution 7 - C#

Ctrl + F5 is better, because you don't need additional lines. And you can, in the end, hit enter and exit running mode.

But, when you start a program with F5 and put a break-point, you can debug your application and that gives you other advantages.

Solution 8 - C#

Those solutions mentioned change how your program work.

You can off course put #if DEBUG and #endif around the Console calls, but if you really want to prevent the window from closing only on your dev machine under Visual Studio or if VS isn't running only if you explicitly configure it, and you don't want the annoying 'Press any key to exit...' when running from the command line, the way to go is to use the System.Diagnostics.Debugger API's.

If you only want that to work in DEBUG, simply wrap this code in a [Conditional("DEBUG")] void BreakConditional() method.

// Test some configuration option or another
bool launch;
var env = Environment.GetEnvironmentVariable("LAUNCH_DEBUGGER_IF_NOT_ATTACHED");
if (!bool.TryParse(env, out launch))
	launch = false;

// Break either if a debugger is already attached, or if configured to launch
if (launch || Debugger.IsAttached) {
	if (Debugger.IsAttached || Debugger.Launch())
		Debugger.Break();
}

This also works to debug programs that need elevated privileges, or that need to be able to elevate themselves.

Solution 9 - C#

If you do not want the program to close even if a user presses anykey;

 while (true) {
      System.Console.ReadKey();                
 };//This wont stop app

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionZigndView Question on Stackoverflow
Solution 1 - C#AdamView Answer on Stackoverflow
Solution 2 - C#Silvia ZView Answer on Stackoverflow
Solution 3 - C#Sohail xIN3NView Answer on Stackoverflow
Solution 4 - C#Darin DimitrovView Answer on Stackoverflow
Solution 5 - C#matthewrView Answer on Stackoverflow
Solution 6 - C#gotqnView Answer on Stackoverflow
Solution 7 - C#Slobodan StankovićView Answer on Stackoverflow
Solution 8 - C#EricView Answer on Stackoverflow
Solution 9 - C#PodTech.ioView Answer on Stackoverflow