When I run my program, the console window seems to run and close. How to keep it open so I can see the results?
class Program
{
public class StringAddString
{
public virtual void AddString()
{
var strings2 = new string[] { "1", "2", "3", "4", "5","6", "7", "8", "9"};
Console.WriteLine(strings2);
Console.ReadLine();
}
}
static void Main(string[] args)
{
StringAddString s = new StringAddString();
}
}
You can handle this without requiring a user input.
Step 1. Create a ManualRestEvent at the start of Main thread
Step 2 . Wait ManualResetEvent
Step 3.To Stop
There are two ways I know of
1)
Console.ReadLine()
at the end of the program. Disadvantage, you have to change your code and have to remember to take it out2) Run outside of the debugger
CONTROL-F5
this opens a console window outside of visual studio and that window won't close when finished. Advantage, you don't have to change your code. Disadvantage, if there is an exception, it won't drop into the debugger (however when you do get exceptions, you can simply just rerun it in the debugger)Use Console.Readline() at the end .Your code will not close until you close it manually.Since Readline waits for input that needs to be entered for your code hence your console will be open until you type some input.
Make sure to use
Console.ReadLine();
to keep the preceedingConsole.WriteLine("");
message from closing.Put a Console.Read() as the last line in your program. That will prevent it from closing until you press a key
If you want to keep it open when you are debugging, but still let it close normally when not debugging, you can do something like this:
Like other answers have stated, the call to
Console.ReadLine()
will keep the window open until enter is pressed, butConsole.ReadLine()
will only be called if the debugger is attached.