-
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?
-
Can we see your message loop, or at least the part that handles WM_TIMER?
-
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))
-
If you want to stop a timer, kill it.
Code:
BOOL KillTimer(
HWND hWnd,
UINT_PTR uIDEvent
);
-
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.
-
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.
-
This another one of Microsofts Bright Ideas
-
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.
-
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.
-
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