Alex Rivera | Logout

Wait for Keypress (or) N Seconds to Expire

Asked 2012-08-02T20:50:34.363
8

Suppose my console program performs multiple lengthy tasks. Between these tasks, I'd like it to pause - either for a few seconds or until any key gets pressed. (Whichever comes first.)

These criteria are easy to check on their own, but they refuse to get along when I try combining them: Either the timing mechanism pauses for a few seconds before ReadKey starts, or Readkey blocks out the thread entirely until satisfied. How can I satisfy both conditions?

Edit
Report

1 Answer

0

Something like this?

bool IsPaused;
bool IsKeyPaused;

void Pause(){}
void UnPause(){}

void KeyDown(){
    if(!IsPaused)
    {
        IsPaused = true;
        IsKeyPaused = true;
        Pause();
    }
}
void KeyUp(){
    if(IsKeyPaused)
    {
        IsPaused = false;
        IsKeyPaused = false;
        UnPause();
    }
}
void TimerPause(){
    if(!IsPaused)
    {
        IsPaused = true;
        Pause();
    }
}
void TimerEnd(){
     UnPause();
}
answered 2012-08-02T21:08:04.570

Your Answer