Professional Documents
Culture Documents
C# - Ways of Console Pausing - Stack Overflow
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 2minute 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 selfmade 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
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/waysofconsolepausing 1/4
29/04/2015 c# Ways of console pausing Stack Overflow
thinking about real problems. This is a case of microoptimization and premature optimization, both of which
are bad. – siride Dec 1 '12 at 17:42
3 Answers
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
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)
http://stackoverflow.com/questions/13661890/waysofconsolepausing 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/waysofconsolepausing 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 builtin "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/waysofconsolepausing 4/4