Windows Phone Developers

Wednesday, October 22, 2008

C# Console Application – Pause for User Input / Pause Command in C# Console Application

In some cases you may require to pause for the user input or display a message to the user and prompt him to continue by pressing the key.

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 Digg Technorati Delicious StumbleUpon Reddit BlinkList Furl Mixx Facebook Google Bookmark Yahoo
ma.gnolia squidoo newsvine live netscape tailrank mister-wong blogmarks slashdot spurl StumbleUpon

9 comments:

  1. It is really a great help for a C# beginner like me. Thank you very much.

    ReplyDelete
  2. Great simple application.

    I'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?

    ReplyDelete
  3. 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

    ReplyDelete
  4. Thanks for the link.

    I'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.

    ReplyDelete
  5. 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

    ReplyDelete
  6. suppose if i want exit the console after pressing any key..then how do i do that?

    ReplyDelete
  7. i want to know that what is console applction?

    ReplyDelete