ReadKey does the trick. It obtains the next character or function key pressed by the user.
Console.WriteLine("Press any key to continue...");
Console.ReadKey(true);
If you want the key to be displayed on user window, you can set the parameter to be false. You can also get the pressed key using as ConsoleKeyInfo shown below
Console.WriteLine("Press any key to continue...");
System.ConsoleKeyInfo KInfo = Console.ReadKey(true);
Console.WriteLine("You have pressed :" + KInfo.Key.ToString());
Press any key to continue in C#, C# Console Application Wait, Wait for user input in C# Console Application, C# Console Application, C# ConsoleKeyInfo, C# KeyPress in Console Application
Press any key to continue in .NET, .NET Console Application Wait, Wait for user input in .NET Console Application, .NET Console Application
C# Console Application Wait
It is really a great help for a C# beginner like me. Thank you very much.
ReplyDeleteGreat simple application.
ReplyDeleteI'd also like to add something to it, but don't know how.
I want the application to do something while it is waiting for the Console input (e.g. play sound). How can I do that?
If you require a beep and pause in console application, you can try http://dotnetdud.blogspot.com/2008/12/beep-in-c-console-application.html
ReplyDeleteThanks for the link.
ReplyDeleteI'm looking for a simple example that shows:
1. Wait for input.
ex: "Press any key to continue."
2. Do something while waiting for input.
ex: Plays sound/music/beep until key is pressed.
You need to use Multithreading Jake. Please try the one I have created for you http://dotnetdud.blogspot.com/2009/01/how-to-use-multithreading-in-c-console.html
ReplyDeleteThanks for sharing...
ReplyDeletesuppose if i want exit the console after pressing any key..then how do i do that?
ReplyDeletei want to know that what is console applction?
ReplyDeletethank u
ReplyDelete