Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

29/04/2015 c# ­ Ways of console pausing ­ Stack Overflow

 
 
 
 
sign up log in tour help  stack overflow careers

Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no
Take the 2­minute tour   ×
registration required.

Ways of console pausing

As I'm new to C#, I searched Google for various stuff which I used to use in C++. One of them is a pause possibility in a console app.

A lot of people suggested different ways like

System.Console.ReadKey(true);
System.Console.WriteLine();

Others even showed self­made functions which 'should' be more efficient than others. And that's a real headache to decide which one is a
better solution.

Could anyone give any examples of how C# interpret them and which way should be the most efficient?

c#   console

edited Dec 1 '12 at 17:53 asked Dec 1 '12 at 17:36
Picrofo Software RnD
3,547 2 9 32 248 1 9 31

4   If I may ask ­ why should pausing be efficient!? –  Kobi Dec 1 '12 at 17:38

    I know that it doesn't make huge different in small apps, but thinking theoretical, which is better. –   RnD


Dec 1 '12 at 17:39

    Why would you pause it is the first question you should ask yourself. –  CSharpie Dec 1 '12 at 17:40

    Homework assignment, need to print out something in the end of calculations, can't do that without pausing
it, right? –   RnD  Dec 1 '12 at 17:41 

4   @RnD: the exercise is pointless. There will never be some theoretical situation where efficient pausing is
necessary, and even if there were some corner case where it was, you'd be better off spending your time
http://stackoverflow.com/questions/13661890/ways­of­console­pausing 1/4
29/04/2015 c# ­ Ways of console pausing ­ Stack Overflow
thinking about real problems. This is a case of micro­optimization and premature optimization, both of which
are bad. –  siride Dec 1 '12 at 17:42

3 Answers

I usually use  do  and  while  to pause the console. Then, if necessary, the console should


resume if you press a specific key.

Example

do
{

/*  while (!Console.KeyAvailable) //Continue if pressing a Key press is not available 
in the input stream
    {
        //Do Something While Paused
    } 
*/

} while (Console.ReadKey(true).Key != ConsoleKey.Escape); //Resume if Escape was 
pressed

If you leave this as  //Do Something While Paused , the console will only resume if the  Esc  key


was pressed doing nothing while paused.

However, if you would not like the console application to resume, you can use  while (true);
instead of  while (Console.ReadKey(true).Key != ConsoleKey.Escape);

Example

do
{
    //break; //Resume
} while (true); //Continue while True (Always True)

Notice : The console application will pause because by doing  do { } while (Condition);  you


are simply telling the console application that you are doing something. So, the console

http://stackoverflow.com/questions/13661890/ways­of­console­pausing 2/4
29/04/2015 c# ­ Ways of console pausing ­ Stack Overflow

application will wait for the operation to execute. Then, normally close when there's nothing to do. 
Notice : The  while  is used to loop. So, the application will not close unless the condition
becomes false.

Thanks, 
I hope you find this helpful :)

edited Dec 1 '12 at 18:01 answered Dec 1 '12 at 17:49
Picrofo Software
3,547 2 9 32

Or you can use what Pat did but for Arguments instead of

Arguments = "/C pause"

you can use

Arguments = "/C TIMEOUT /t 4 /nobreak > NUL"

where number 4 is number of seconds console will pause before executing rest of the
program.

And the whole function would be

static void Pause(int sec)
    {
        Console.WriteLine();
        var pauseProc = Process.Start(
            new ProcessStartInfo()
            {
                FileName = "cmd",
                Arguments = "/C TIMEOUT /t " + sec + " /nobreak > NUL",
                UseShellExecute = false
            });
        pauseProc.WaitForExit();
    }

and you will call it with Pause function with number of seconds to pause.

http://stackoverflow.com/questions/13661890/ways­of­console­pausing 3/4
29/04/2015 c# ­ Ways of console pausing ­ Stack Overflow
Pause(4);

Hope it helps.

answered Jul 23 '14 at 8:22
Amir Causevic
19 4

If you're talking about the built­in "pause" command, you could always call it ­­ even though it's
ugly. Here's what I use:

static void Pause()
{
    Console.WriteLine();
    var pauseProc = Process.Start(
        new ProcessStartInfo()
            {
                FileName = "cmd",
                Arguments = "/C pause",
                UseShellExecute = false
            });
    pauseProc.WaitForExit();
}

Normally like so:

if (Environment.UserInteractive())
    Pause();

Hope this helps.

edited Jun 7 '13 at 10:15 answered Jun 7 '13 at 10:06
Pat
718 4 12

http://stackoverflow.com/questions/13661890/ways­of­console­pausing 4/4

You might also like