6

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?

Picrofo Software
  • 5,475
  • 3
  • 23
  • 37
RnD
  • 1,019
  • 5
  • 23
  • 49
  • 6
    If I may ask - why should *pausing* be efficient!? – Kobi Dec 01 '12 at 17:38
  • I know that it doesn't make huge different in small apps, but thinking theoretical, which is better. – RnD Dec 01 '12 at 17:39
  • Why would you pause it is the first question you should ask yourself. – CSharpie Dec 01 '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 01 '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 thinking about real problems. This is a case of micro-optimization and premature optimization, both of which are bad. – siride Dec 01 '12 at 17:42

5 Answers5

8

Run the program using any of the following methods:

  1. ctrl + F5

OR as Rajesh suggested

  1. Console.ReadKey() //pauses for any key
  2. Console.ReadLine() //pauses for enter key
static_cast
  • 1,174
  • 1
  • 15
  • 21
2

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

Picrofo Software
  • 5,475
  • 3
  • 23
  • 37
2

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.

Pat Hermens
  • 906
  • 6
  • 19
1

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.

Pause(4);

Hope it helps.

-1

you can write "Console.ReadLine();" this too for pupose.

  • 2
    This is already covered in the [answer by Rajesh](//stackoverflow.com/a/31239421/1677912). If you found an answer helpful, you should [upvote that answer](//stackoverflow.com/help/privileges/vote-up) when you have earned that privilege. – Mogsdad Aug 13 '15 at 19:08