Press Q to Quit (console app)

  • Thread starter Thread starter WildHare
  • Start date Start date
W

WildHare

I have a console application that I want to continue to run until I press a
key.

How can I get a keypress without stopping execution?

Thanks.
 
Console.Read() will wait until a key is pressed. You can get the input and test to see if the character is Q and quit or read another character.
 
But that is the point, I don't want the application to stop. I want the app
(in a loop) to keep running UNTIL I press 'Q'.


Tim Begin said:
Console.Read() will wait until a key is pressed. You can get the input
and test to see if the character is Q and quit or read another character.
 
Hmm, you could start a new thread that executes your buisness logic, and
have your main thread wait for input, when the input comes you could kill
the buisness thread, is that what you wanted?
 
WildHare said:
But that is the point, I don't want the application to stop. I want the app
(in a loop) to keep running UNTIL I press 'Q'.




and test to see if the character is Q and quit or read another character.

This isn't supported in the 1.x Framework Console class. I believe that
it is supported in the upcoming 2.0 Framework (I know - that doesn't
help you today...).

The solution that others have mentioned working involves using P/Invoke
to the Win32 Console APIs. I have no sample for you, as I haven't
needed to do this, but I'm sure that a quick Google would net some good
results.

Or just write the P/Invoke wrappers yourself, it can't be all that complex.
 
My head is spinning with all the paths of execution (the business logic
launches all finds of threads as part of its behavior), but I will find a
way to document that.

The only remaining issue is that I have to press <enter> for the Q to be
read. I guess, unless I want to make API calls I am stuck with that ...
right?
 
Back
Top