Alternative to Console.ReadLine () to keep the console visible

Is there a prettier statement (or a way) to prevent the console from disappearing than the hacker call Console.ReadLine (). Something more expressive of a goal more orthogonal to keep the Console visible?

+4
source share
5 answers

If you are still developing an application, you can run it via Ctrl + F5 (without debugging), otherwise you can use Console.ReadKey () (the same, but no longer an option)

+7
source

You can do:

Console.ReadKey(); 

Console.ReadLine() not very hacky, your pause on the screen to wait for input. An input can be either a single key or a string.

Update

One of the great features of the ReadKey () method is that it "waits, that is, blocks the thread issuing the ReadKey method until a key or function key is pressed." MSDN

This is different from ReadLine, which accepts a string. Possibly cleaner.

+5
source

It depends on the context. If you are talking about starting the command line, debugging through your code, and after that you can view the results on the console, you have two options:

  • If you run the attached debugger (f5), you should use Console.ReadLine
  • If you run the application without debugging (ctrl + f5), it will remain open ... but then you obviously cannot debug it.

I'm not sure why this is the default behavior, but there it is :-)

+2
source

I usually use one of them:

 Console.ReadKey(true); //You might want to put this in an infinite loop new AutoResetEvent(false).WaitOne(); 

In VS, you can also run ( Ctrl + F5 ) a program (as opposed to working in debugging), and it will add a system pause after completion of its completion.

I would say that WaitOne just works (& not debugging) the program - these are your non-hacker options.

If you want to debug, maybe set a breakpoint at the last } .

0
source

Depending on what I do. If I am doing multi-threaded work and want my console application to stay alive until all other work is done, I usually do something like this. (Similar to MasterMastic)

 using System; using System.Threading; namespace Test_Console { class Program { static EventWaitHandle EWHandle; static void Main(string[] args) { EWHandle = new EventWaitHandle(false, EventResetMode.AutoReset); Thread WorkThread = new Thread(new ThreadStart(DoStuff)); EWHandle.WaitOne(); } static void DoStuff() { Console.WriteLine("Do what you want here"); EWHandle.Set(); } } } 

Of course, regular breakpoints and other debugging tools are always used, if that's what you are going to do.

0
source

All Articles