How to keep console window open

别说谁变了你拦得住时间么 提交于 2019-11-26 07:47:49

问题


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();            
    }
}

回答1:


You forgot calling your method:

static void Main(string[] args)
{          
    StringAddString s = new StringAddString();  
    s.AddString();
}

it should stop your console, but the result might not be what you expected, you should change your code a little bit:

Console.WriteLine(string.Join(",", strings2));



回答2:


Put a Console.Read() as the last line in your program. That will prevent it from closing until you press a key

static void Main(string[] args)
{
    StringAddString s = new StringAddString();
    Console.Read();            
}



回答3:


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:

if (System.Diagnostics.Debugger.IsAttached) Console.ReadLine();

Like other answers have stated, the call to Console.ReadLine() will keep the window open until enter is pressed, but Console.ReadLine() will only be called if the debugger is attached.




回答4:


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 out

2) 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)




回答5:


Console.ReadKey(true);

This command is a bit nicer than readline which passes only when you hit enter, and the true parameter also hides the ugly flashing cursor while reading the result :) then any keystroke terminates




回答6:


Write Console.ReadKey(); in the last line of main() method. This line prevents finishing the console. I hope it would help you.




回答7:


You can handle this without requiring a user input.

Step 1. Create a ManualRestEvent at the start of Main thread

ManualResetEvent manualResetEvent = new ManualResetEvent(false);

Step 2 . Wait ManualResetEvent

manualResetEvent.WaitOne();

Step 3.To Stop

manualResetEvent.Set();



回答8:


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.




回答9:


Make sure to useConsole.ReadLine(); to keep the preceeding Console.WriteLine(""); message from closing.




回答10:


For visual c# console Application use:

Console.ReadLine();
Console.Read();
Console.ReadKey(true);

for visual c++ win32 console application use:

system("pause");

press ctrl+f5 to run the application.




回答11:


If your using Visual Studio just run the application with Crtl + F5 instead of F5. This will leave the console open when it's finished executing.




回答12:


To be able to give it input without it closing as well you could enclose the code in a while loop

while (true) 
{
     <INSERT CODE HERE>
}

It will continue to halt at Console.ReadLine();, then do another loop when you input something.



来源:https://stackoverflow.com/questions/16952846/how-to-keep-console-window-open

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!