What happens when code in Notify() of wxTimer takes longer than the timer interval? Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
macsinus
Earned a small fee
Earned a small fee
Posts: 16
Joined: Mon Feb 22, 2016 12:36 pm
Location: Leipzig, Germany

What happens when code in Notify() of wxTimer takes longer than the timer interval?

Post by macsinus »

Is the next Notify() suppressed, queued or executed "simultaneously"?
Can/should I check for this case?
Would it be OK instead to use a single-shot timer with a StartOnce() at the end of the Notify()?
I have my doubts about the latter because the documentation states that "A timer can only be used from the main thread".
Thanks in advance!
Last edited by macsinus on Wed Feb 24, 2016 8:00 am, edited 2 times in total.
eranif
Moderator
Moderator
Posts: 610
Joined: Tue Nov 29, 2005 7:10 pm
Location: Israel

Re: What happens when code in Notify() of wxTimer takes longer than the timer interval?

Post by eranif »

Usually, I use a 'one-shot' timer and trigger another one in the handler itself.
This way I assure that the timer starts again once the handler code is done.
I have my doubts about the latter because the documentation states that "A timer can only be used from the main thread".
I don't see any problem with this, since the handler is also called in the main thread

Eran
IDE: CodeLite + wxCrafter
OS: All
https://wxcrafter.codelite.org
https://codelite.org
User avatar
T-Rex
Moderator
Moderator
Posts: 1248
Joined: Sat Oct 23, 2004 9:58 am
Location: Zaporizhzhya, Ukraine
Contact:

Re: What happens when code in Notify() of wxTimer takes longer than the timer interval?

Post by T-Rex »

On Windows the timer window message has the lowest priority and when you start the timer, the system does not guarantee that the message will be sent if the thread's event loop is busy processing other window messages. So if your timer window message handler works longer than the timer's interval, most likely you will not get the next timer message.
And since the main thread has only one event loop, then your window procedure will not get the next message until you process the current one, I assume.
macsinus
Earned a small fee
Earned a small fee
Posts: 16
Joined: Mon Feb 22, 2016 12:36 pm
Location: Leipzig, Germany

Re: What happens when code in Notify() of wxTimer takes longer than the timer interval?

Post by macsinus »

Hello Eran,
thank you for your quick reply and for the good news that the "chained" one-shot timer solution works - I'll go along this route.
Somehow I had assumed that the Notify() would be executed within a different thread.
Now that I know that it is in the main thread I don't need to have any headaches regarding display updating either. :D
Thanks also T-Rex; another good reason to do it this way.
macsinus
Earned a small fee
Earned a small fee
Posts: 16
Joined: Mon Feb 22, 2016 12:36 pm
Location: Leipzig, Germany

Re: What happens when code in Notify() of wxTimer takes longer than the timer interval?

Post by macsinus »

Just to confirm - I've now implemented this so and it works like a charm.
Thanks again!
wxWidgets 3.1.0
Boost 1.65
VS 2017
Win 7
Post Reply