Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Developer
Posts: 68
Registered: ‎03-12-2013
My Device: BB Z10
My Carrier: Rogers
Accepted Solution

QTimer sometimes fires very late!

I posted recently about a QTimer start(0) bug, so I changed my code to instead start the timer with a short interval of 100 milliseconds. Usually this works fine, but there are times when the timer doesn't fire until 10 seconds go by! Looking at the log file shows that there doesn't seem to be any other activity going on during this time. Is there a known issue here?

 

QTimer GuiTaskQueue::s_timer;

 

GuiTaskQueue::GuiTaskQueue() : super(NULL)
{

  connect(&s_timer, SIGNAL(timeout()), this, SLOT(onTimer()));

  s_timer.setSingleShot(true);
}

 

when I start the timer:

s_timer.start(100); // milliseconds

 

the timer is not stopped (it's one-shot)

Retired
Posts: 47
Registered: ‎04-19-2013
My Device: I love them all!
My Carrier: Carriers rock!

Re: QTimer sometimes fires very late!

Do you have anything else running on your event thread while this is going on? Remember that the timer notifications are delivered on the event thread and blocking it will prevent you from processing them.
Developer
Posts: 68
Registered: ‎03-12-2013
My Device: BB Z10
My Carrier: Rogers

Re: QTimer sometimes fires very late!

We have logging around the methods that can be called from UI events, timers, etc. (as well as almost all of the lower level calls made by these methods) and during this period nothing is logged so I am reasnably confident that there is nothing going on during this period that should cause the timer not to fire.

Developer
Posts: 68
Registered: ‎03-12-2013
My Device: BB Z10
My Carrier: Rogers

Re: QTimer sometimes fires very late!

Here's what it looks like after investigating this for a while... when the timer is started from a non-GUI thread it won't fire until something happens on the GUI thread. Whether it is another timer that fires or whether it's the user taking some action, it seems that the timer that is started on the non-GUI thread needs some UI thread activtity before it will fire. This looks like a serious OS bug.

Retired
Posts: 47
Registered: ‎04-19-2013
My Device: I love them all!
My Carrier: Carriers rock!

Re: QTimer sometimes fires very late!

So, you have two threads, A and B, and thread A declares and starts a QTimer, but you're concerned the events aren't being delivered on thread B? Did you move the QTimer to the other thread? Since QTimer events are delivered via the event queue, they have thread affinity and can't be utilized lightly across thread boundaries.
Developer
Posts: 68
Registered: ‎03-12-2013
My Device: BB Z10
My Carrier: Rogers

Re: QTimer sometimes fires very late!

I have the GUI thread and a few other "background" threads. I create the timer on the GUI thread when the application starts. During the program execution the background threads periodically start the timer with a value of X. I expect the timer to fire in X milliseconds (approximately) from when the background thread called "start". I find that the timer is not actually fired until X milliseconds has passed AND THEN something has occured on the GUI thread such as the user took some action, or some other timer fired. If nothing is happening on the GUI thread (i.e. the application is idle) then the timer will not fire - until something ends up happening on the GUI thread. I have other timers going off so these seem to trigger the subject timer, as does user activity.

Retired
Posts: 47
Registered: ‎04-19-2013
My Device: I love them all!
My Carrier: Carriers rock!

Re: QTimer sometimes fires very late!

Are you attempting to start the timer from the blocked thread?
Developer
Posts: 68
Registered: ‎03-12-2013
My Device: BB Z10
My Carrier: Rogers

Re: QTimer sometimes fires very late!

In many cases the thread that is calling "start" on the timer will soon thereafter call Wait() on a Semaphore. I haven't looked at whether this problem only occurs in these cases but it is an interesting possibility.

Retired
Posts: 47
Registered: ‎04-19-2013
My Device: I love them all!
My Carrier: Carriers rock!

Re: QTimer sometimes fires very late!

If you can create a simple app to demonstrate the problem, I would be willing to take a look. I would encourage you to invert your task processing order, though. Move the heavy lifting off to another thread (fairly simple to do through QtConcurrent::run()) and leave the event thread processing, as then your UI will remain fluid.
Developer
Posts: 68
Registered: ‎03-12-2013
My Device: BB Z10
My Carrier: Rogers

Re: QTimer sometimes fires very late!

The heavy processing is currently being done by the background thread and then the GUI thread is only dealing with the result via the timer so I think I already meet your suggestion, unless I misunderstood.