SetTimer doesn't work
-
Hi All, I used the following code. But it appears that my code in the OnTimer function is never executed. The strangest thing is that if I click the menu ,so it drops down, the timer is executed! And as soon as I pull up the menu again, the timer stops! Can anyone give me a explaination for this? Thanks a lot. void CMyDialog::OnMyFunction() { ... SetTimer(1, 500, NULL); ... } void CMyDialog::OnTimer(UINT nIDEvent) { ... // my code} }
-
Hi All, I used the following code. But it appears that my code in the OnTimer function is never executed. The strangest thing is that if I click the menu ,so it drops down, the timer is executed! And as soon as I pull up the menu again, the timer stops! Can anyone give me a explaination for this? Thanks a lot. void CMyDialog::OnMyFunction() { ... SetTimer(1, 500, NULL); ... } void CMyDialog::OnTimer(UINT nIDEvent) { ... // my code} }
-
Have you a handler for WM_TIMER in message map? BEGIN_MESSAGE_MAP... ... ON_WN_TIMER() END_MESSAGE_MAP try to use VERIFY(SetTimer(1,500,NULL)); to make sure you've set timer
Yes, I have the handler in message map. what does the VERIFY() should do? because nothing different happened when I add it. The line of code right before the SetTimer(1,500,NULL) is: MCIWnd(hMCI); // plays a movie This shouldn't affect anything, should it? I know that MSDN says "The WM_TIMER message is a low-priority message." I have another smaller app that also dialog-based that does that: plays a movie while activating the timer. Thanks.
-
Yes, I have the handler in message map. what does the VERIFY() should do? because nothing different happened when I add it. The line of code right before the SetTimer(1,500,NULL) is: MCIWnd(hMCI); // plays a movie This shouldn't affect anything, should it? I know that MSDN says "The WM_TIMER message is a low-priority message." I have another smaller app that also dialog-based that does that: plays a movie while activating the timer. Thanks.
-
Yes, I have the handler in message map. what does the VERIFY() should do? because nothing different happened when I add it. The line of code right before the SetTimer(1,500,NULL) is: MCIWnd(hMCI); // plays a movie This shouldn't affect anything, should it? I know that MSDN says "The WM_TIMER message is a low-priority message." I have another smaller app that also dialog-based that does that: plays a movie while activating the timer. Thanks.
-
VERIFY does make sure the function returns value non-zero, otherwise it popups assertion dialog box in DEBUG, in Release does nothing. You, probably, right speaking about low-priority. You may create separate thread for handling WM_TIMER.
I never use thread before (and it sounds&seems confusing) :) Could you please post a small sample code on how to create, call, and destroy a simple thread on an MFC dialog-based? Thanks a lot in advance.
-
Hi All, I used the following code. But it appears that my code in the OnTimer function is never executed. The strangest thing is that if I click the menu ,so it drops down, the timer is executed! And as soon as I pull up the menu again, the timer stops! Can anyone give me a explaination for this? Thanks a lot. void CMyDialog::OnMyFunction() { ... SetTimer(1, 500, NULL); ... } void CMyDialog::OnTimer(UINT nIDEvent) { ... // my code} }
Try running it through the debugger and trace your code. Something might make itself clear to you. ~Andy
-
Try running it through the debugger and trace your code. Something might make itself clear to you. ~Andy
I did some more digging and found something: In the project, I create an owner draw button. What I want to do is move this button while the movie is playing. Therefore I add this line on OnTimer: GetDlgItem(IDC_MY_IMAGE_BUTTON)->MoveWindow(newX, newY, newWidth, newHeight, true); And it works (it moves while the movie playing). But I also want to draw an image on the button, so I created a new class CMyImageButton, derived from CButton class. And as soon as I associated the owner draw button variable to CMyImageButton class, the OnTimer stop working. Any idea why? Thanks.
-
I did some more digging and found something: In the project, I create an owner draw button. What I want to do is move this button while the movie is playing. Therefore I add this line on OnTimer: GetDlgItem(IDC_MY_IMAGE_BUTTON)->MoveWindow(newX, newY, newWidth, newHeight, true); And it works (it moves while the movie playing). But I also want to draw an image on the button, so I created a new class CMyImageButton, derived from CButton class. And as soon as I associated the owner draw button variable to CMyImageButton class, the OnTimer stop working. Any idea why? Thanks.
Many moons ago I remember having all sorts of trouble with SetTimer and seem to remember thatI ended up fixing them by changing the event number ie SetTimer(111,500,NULL); I never worked out why this worked and 1 didn't but it may be worth a try in your case.
-
Many moons ago I remember having all sorts of trouble with SetTimer and seem to remember thatI ended up fixing them by changing the event number ie SetTimer(111,500,NULL); I never worked out why this worked and 1 didn't but it may be worth a try in your case.
If you are using SetTimer() with a NULL 3rd argument you are relying on the message queue sending you the lowest priority message going to tell you about the timer. Not good. Even worse is if you are busy then no messages are being processed, hence your timer will never be triggered. This is why when you click the menu, you caused some processing of the message queue to happen, you get your timer event. Solution 1: Pass a callback function for parameter 3. This will be called much more reliably. Solution 2: Go multi threaded (beyond the scope of me typing just before going home). There are various articles on codeproject or you could take a look at some of the Platform SDK samples. I've used both 1 and 2 depending on the situtation (and programming model - Win16 only solution 1 is available). Cheers Stephen Kellett