CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Mar 2002
    Location
    Philadelphia
    Posts
    150

    C# getch() & "Press any key to end..."

    What happened to getch() in C#? getch() is very handy when you are debuging a C++ console app. I would usually end my C++ programs as such:
    Code:
    main(//...
    {
    	//...
    	cout << "Press any key to end..." << endl << flush;
    	_getch();
    }
    However, C#'s Console.Read does not seem to work the same way.
    Code:
    static void Main(string[] args)
    {
    	//...
    	Console.WriteLine("Press any key to end...");
    	Console.Read();
    }
    When I press keys other than the <enter> key it echoes them to the console instead of returning from Console.Read(). How can I implement "Press any key to end..." in C#? Please help.
    Sincerely,
    - Ron

  2. #2
    Join Date
    Dec 2003
    Location
    http://map.search.ch/zuerich.en.html
    Posts
    1,074
    Doesn't appear to be part of C#, but can be accessed from the msvcrt.dll.

    Code:
    using System;
    using System.Runtime.InteropServices;
    
    	class HelloWorld
    	{
    			[DllImport("msvcrt.dll")]
    			static extern int _getch();
    			
    		static void Main(string[] args)
    		{
    			Console.WriteLine("Hello World");
    			Console.WriteLine("Press any key to end...");
    			_getch();
    		}
    	}

  3. #3
    Join Date
    Dec 2008
    Posts
    1

    Re: C# getch() & "Press any key to end..."

    I know this is a very old thread, but it's #3 on Google for c# "press any key"

    You needn't import an external DLL for this.

    Console.ReadKey(true);

    will do the trick just fine.

  4. #4
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: C# getch() & "Press any key to end..."

    Who many times does this have to be posted....

    A Console Application is supposed to immediately exit upon cpmpleting its work. It is WRONG to put ANY type of artifical construct which makes the user hit a key.

    1) If you are debugging the application. Put a break point on the closing brace of main.

    2) If you are running the application and want to see the final output...run it FROM a console..

    3) If you MUST support functionallity of clicking in Explorer, then either use a CMD file or properly set up the propertied of a shortcut.

    If you hard code a requirement for user input, you are violating conventions, and it willhave negative impact when the program is used in scenarios such as batch files or scripts.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  5. #5
    Join Date
    Nov 2007
    Location
    .NET 3.5 / VS2008 Developer
    Posts
    624

    Re: C# getch() & "Press any key to end..."

    Quote Originally Posted by TheCPUWizard View Post
    Who many times does this have to be posted....

    A Console Application is supposed to immediately exit upon cpmpleting its work. It is WRONG to put ANY type of artifical construct which makes the user hit a key.

    1) If you are debugging the application. Put a break point on the closing brace of main.

    2) If you are running the application and want to see the final output...run it FROM a console..

    3) If you MUST support functionallity of clicking in Explorer, then either use a CMD file or properly set up the propertied of a shortcut.

    If you hard code a requirement for user input, you are violating conventions, and it willhave negative impact when the program is used in scenarios such as batch files or scripts.
    I have to disagree. There are many, many applications that the UI is actually the console window. Also, if processing data, it's good to let the user know that it has completed successfully(or unsuccessfully) and have them click a button to exit. Just exiting without showing the user that something was done is bad design.....in my opinion.

  6. #6
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: C# getch() & "Press any key to end..."

    Quote Originally Posted by eclipsed4utoo View Post
    I have to disagree. There are many, many applications that the UI is actually the console window. Also, if processing data, it's good to let the user know that it has completed successfully(or unsuccessfully) and have them click a button to exit. Just exiting without showing the user that something was done is bad design.....in my opinion.
    And how are you going to "click a button" on a console application???

    Consider how all of the "build-in" commands work [eg. xcopy]. As it copies, it displays the files, and a total count when complete...then IMMEDIATELY exits back to the prompt. A perfect example of a command that shows potentially important information and also immediately exits.

    There ARE character based GUI application (technically console applications with cursor positioning), but these are NOT the types of programs that we are discussing.

    An acceptable alternative (which I have previously posted about - probably in a different thread) is to add a command line switch (typically /P) which will cause a pause.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  7. #7
    Join Date
    Mar 2008
    Location
    IRAN
    Posts
    811

    Re: C# getch() & "Press any key to end..."

    i think that TheCPUWizard is correct; because i have seen many professional console application that after doing their job exit immediatly; if user want to see the end result he/ she can access the application using CMD. it seems that this behaviour is a convention that standard that developers should mention to it.
    Please rate my post if it was helpful for you.
    Java, C#, C++, PHP, ASP.NET
    SQL Server, MySQL
    DirectX
    MATH
    Touraj Ebrahimi
    [toraj_e] [at] [yahoo] [dot] [com]

  8. #8
    Join Date
    Mar 2015
    Posts
    1

    Re: C# getch() & "Press any key to end..."

    Quote Originally Posted by TheCPUWizard View Post
    Who many times does this have to be posted....
    I'm laughing at you seven years in the future.

  9. #9
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: C# getch() & "Press any key to end..."

    Quote Originally Posted by azbishop View Post
    I'm laughing at you seven years in the future.
    Not sure what you are laughing about, but I am still here.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  10. #10
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: C# getch() & "Press any key to end..."

    Don't poke the bear. Welcome back cpu.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured