Timer not working
-
Hello,
I am using qtimer having 500 ms time value in my project.
While performing file sorting operation,I observed that timer expired event not generated.
It means timer slot get blocked till file operation complete.I will appreciate your quick response.
Thanks. -
Hi,
how did you start the sorting operation? The timer fires the execution of a slot. If the file sorting is done in another already running slot, the timer slot will execute after the file sorting is finished as all slots work one after the other: its a message queue, after all.
You may need to do the file sorting in another thread to make the timer slot run in parallel.
-Michael. -
Hello,
I am using qtimer having 500 ms time value in my project.
While performing file sorting operation,I observed that timer expired event not generated.
It means timer slot get blocked till file operation complete.I will appreciate your quick response.
Thanks.@Rashmi Most probably your sorting operation is lasting for long time and blocks the event loop :-)
For example this loop will block event loop until finished:while(SOME_CONDITION) { }
As long as the event loop is blocked your slot will not be executed.
To avoid such problems you should avoid blocking operations in Qt (actually in all event-driven frameworks). If you cannot avoid them then execute them in a different thread. -
Hello,
I am using qtimer having 500 ms time value in my project.
While performing file sorting operation,I observed that timer expired event not generated.
It means timer slot get blocked till file operation complete.I will appreciate your quick response.
Thanks.@jsulm said in Timer not working:
As long as the event loop is blocked your slot will not be executed.
Even something more, you'll get a single timer event after the whole interval. As explained in the docs:
All timer types may time out later than expected if the system is busy or unable to provide the requested accuracy. In such a case of timeout overrun, Qt will emit activated() only once, even if multiple timeouts have expired, and then will resume the original interval.