views:

20

answers:

1

I have a console application which is intended to just keep running until it is killed. Essentially:

<setupCode> 

Do 
    <doProcessing>
    Thread.Sleep(<interval>)
Loop

Essentially this will keep running until the user kills it, obviously.

What I'd like to do is replace the Thread.Sleep call with a wait-condition waiting on either one of two separate events... something like

WaitFor(<intervalPasses> Or <KeyPress>)

so that the app will basically just sleep in the background until the next interval passes, but so that the user can "wake it up" with a keypress. I also want to be able to get the information about the keypress so that e.g. if they pressed Enter I can just start the next iteration without waiting for the timer and if they pressed Escape i would exit the loop altogether, and if they press anything else I would continue waiting for either the timer or one of those two keys.

Either (waiting for a timer) OR (waiting for a keypress) would be easy.
Waiting for either (a timer OR a keypress) I am not sure how to do.

Thanks in advance.

+1  A: 

This is how Windows Forms and WPF work. I'd strongly recommend you use one of them to implement this task. Use a Timer, implement its Tick event. And implement the form's KeyDown event. Something like that, your question is too generic to be more specific.

Hans Passant
I was hoping to keep it as a small, light-weight console app as it doesn't really have any user-interaction and just does background processing, if possible.
eidylon
Meh, I guess I will go that route... it will give me other flexibility and options, so it'll be a good trade-off I guess. Thanks for the idea.
eidylon