Search code examples
c++winapitimervirtualkey

Stopping a Timer With Virtual Keys


I've created a basic window which handles virtual keys. The case VK_UP initializes a timer which is counting from 9 backwards. The timer can be stopped if either 0 is reached or VK_DOWN is pressed. The problem is, when I initialize the timer with VK_UP the program stops until the timer has reached 0. How can I interrupt( stop ) the timer with VK_DOWN?

As always, I've tried search but to no avail.

EDIT: Here's the window procedure:

unsigned Seconds( 9 );
case WM_KEYDOWN:
     {
         switch( wParam )
         {
             case VK_UP:
                  {
                      while( Seconds > 0 )
                      {
                          wait( 1 );
                          Seconds--;
                      }
                  }
                  break;
         }
         break;
     }

Here's the wait( ) method I used above:

void wait( unsigned TotalSeconds )
{
    clock_t Wait( clock( ) + ( TotalSeconds * CLOCKS_PER_SEC ) );
    while( clock( ) < Wait )
    {
        Sleep( 1 );
    }
}

Thanks.


Solution

  • Use a real timer, not a counting loop. Review SetTimer(). Understanding the event driven programming model for Windows is pretty important, it is well covered by any book about Windows programming, like Petzold's.