Thread: Wm_timer

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    76

    Wm_timer

    Hi I am using a windows built timer to add one to a integer every time 3 0 seconds passes. The problem is that once 30 seconds has passed the varible just keeps incrementing itself. Like the WM_TIMER message is continually being called. How can I stop this?

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Can we see your message loop, or at least the part that handles WM_TIMER?
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    76
    You sure can it makes a bit more sense if I show you the entire Message Handler.
    Code:
    LRESULT CALLBACK messageHandler(HWND theHandle, UINT theMsg,
    								WPARAM wParam, LPARAM lParam)
    {
    	// Put all arguements in a switch statement
    	switch(theMsg)
    	{
    	case WM_CREATE:
    		{
    			// CODE ADDED IN REACTION TEST VERSION BETA 3
    			// On createion of the window start one timer
    			// with an ID of 1 and a 2 min 30 span
    			SetTimer(theHandle,1,30000,NULL);
    			// END OF CODE ADDED IN REACTION TEST VERSION BETA 3
    			return 0;
    			break;
    		}
    	case WM_PAINT:
    		{
    			// How to redraw the current window
    			PAINTSTRUCT ps;
    			HDC hdc = BeginPaint(theHandle,&ps);
    			// Extra painting code goes in here
    			EndPaint(theHandle,&ps);
    			return 0;
    			break;
    		}
    	case WM_DESTROY:
    		{
    			// Post quit message when the window is destroyed
    			PostQuitMessage(0);
    			return 0;
    			break;
    		}
    		// CODE ADDED IN REACTION TEST VERSION BETA 1
    	case WM_TIMER:
    		{
    			// put all timers in a switch statement to see
    			// which one went off
    			switch (wParam)
    			{
    			case 1:
    				{
    					// CODE ADDED IN REACTION TEST VERSION BETA 3
    					theGame.getStats().IncrementState();
    					// now check to see if 2 min 30 has passed
    					// and if it has send quit message
    					if ((theGame.getStats().getState()) == 6)
    						PostQuitMessage(0);
    					return 0;
    					break;
    					// END OF CODE ADDED IN REACTION TEST VERSION BETA 3
    				}
    			default:
    				break;
    			}
    		}
    		// END OF CODE ADDED IN REACTION TEST VERSION BETA 1
    	default:
    		{
    			// If the message hasn't been handled let windows
    			// do it
    			return (DefWindowProc(theHandle,theMsg,wParam,lParam));
    			break;
    		}
    	}
    }
    Now what happens is that the programs ends after 30 seconds. WHich means the state must equal 6. Now it shouldn't equal six until 2 and half minutes has passed.

    ((The constructor automatically sets the state to 1))

  4. #4
    Registered User Dante Shamest's Avatar
    Join Date
    Apr 2003
    Posts
    970
    If you want to stop a timer, kill it.

    Code:
    BOOL KillTimer(          
        HWND hWnd,
        UINT_PTR uIDEvent
    );

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    76
    No I dont want to stop it. After 30 seconds it just keep on send out WM_TIMER messages. I just want it to send one than after 30 seconds send another one.

  6. #6
    Registered User
    Join Date
    Jun 2003
    Posts
    245
    It will constantly send out WM_TIMER messager until you kill it.

    To get another 30 seconds once the first 30 seconds has expired, you'll need to kill it, and then setup another timer.

  7. #7
    Registered User
    Join Date
    Feb 2003
    Posts
    76
    This another one of Microsofts Bright Ideas

  8. #8
    Registered User
    Join Date
    Feb 2003
    Posts
    76
    I have sorted it now. No it doesnt constantly send out WM_TIMER messages it only sends one out every 30 seconds. It was my mistake earlier on in the code i had done an if statement and instead of saying does theState equal 5 i sed assign 5 to the value of theState. So when the counter incrmented the value it turned itself off because of course it reached siz. Sorry to waste your time like that.

  9. #9
    Registered User
    Join Date
    Jun 2003
    Posts
    245
    No problem, I thought it worked the way i described, and that is the way I have implemented it in my class that I encapsulate wm_timer around.

  10. #10
    Registered User
    Join Date
    Feb 2003
    Posts
    76
    You would get the same results if you do it your way, but my program would have still set the value to 5 before hand. So it still would have looked like it didnt. Both ways work its just my other programming that is to blame lol

Popular pages Recent additions subscribe to a feed