Reputation: 1069
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?
Upvotes: 6
Views: 42135
Reputation: 993
Putting this out there if anyone wants the same look and feel of Press any key to continue . . .
This one is simple and without any DLL import:
private static void Pause() {
Console.Write("Press any key to continue . . . ");
// ReadKey(Boolean) = true to not display the pressed key
// https://learn.microsoft.com/en-us/dotnet/api/system.console.readkey
Console.ReadKey(true);
}
Or, if you really want the old school flavor (Windows only):
using System.Runtime.InteropServices;
class Whatever {
[DllImport("msvcrt.dll")]
// "system" has to be lowercase
static extern bool system(string str);
static void Main(string[] args) {
system("pause");
}
}
NOTE: Both ReadKey()
and DllImport("msvcrt.dll")
do not respond to pressing a modifier key (Alt, Ctrl, or Shift) by itself. So you're better of just using ReadKey()
.
Upvotes: 0
Reputation: 1178
Run the program using any of the following methods:
OR as Rajesh suggested
Console.ReadKey() //pauses for any key
Console.ReadLine() //pauses for enter key
Upvotes: 8
Reputation:
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.
Upvotes: 1
Reputation: 904
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.
Upvotes: 1
Reputation: 5571
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 :)
Upvotes: 3