Tizen Native API
|
This example shows how to setup timer callbacks. It starts a timer that will tick (expire) every 1 second, and then setup other timers that will expire only once, but each of them will affect the first timer still executing with a different API, to demonstrate its usage. To see the full code for this example, click here.
To demonstrate this, let's define some constants that will determine at which time each timer will expire:
These constants should tell by themselves what will be the behavior of the program, but I'll explain it anyway. The first timer is set to tick every 1 second, but all the other timers until the 6th one will be started concurrently at the beginning of the program. Each of them will expire at the specified time in these constants:
- The timer2, after 3 seconds of the program being executed, will add a delay of 3 seconds to timer1;
- The timer3 will pause timer1 at 8.2 seconds;
- timer4 will resume timer1 at 11.0 seconds;
- timer5 will will change the interval of timer1 to 2 seconds;
- timer6 will stop timer1 and start timer7 and timer8, with 1.1 and 1.2 seconds of interval, respectively; it also sets the precision to 0.2 seconds;
- timer7 and timer8 will just print their expiration time.
As almost all the other examples, we create a context structure to pass to our callbacks, so they can have access to the other timers. We also store the time of the program start in _initial_time
, and use the function _get_current_time
to retrieve the current time relative to that time. This will help demonstrate what is going on.
Now, the behavior and relationship between the timers that was described above is dictated by the following timer callbacks:
It's possible to see the same behavior as other Ecore callbacks here, returning ECORE_CALLBACK_RENEW when the timer needs to continue ticking, and ECORE_CALLBACK_CANCEL when it needs to stop its execution. Also notice that later on our program we are checking for the timers pointers in the context to see if they are still executing before deleting them, so we need to set these timer pointers to NULL
when we are returning ECORE_CALLBACK_CANCEL. Otherwise the pointer would still be not NULL
, but pointing to something that is invalid, since the timer would have already expired without renewing.
Now the main code, which will start the timers:
This code is very simple. Just after starting the library, it will save the current time to _initial_time
, start all timers from 1 to 6, and begin the main loop. Everything should be running right now, displaying the time which each timer is expiring, and what it is doing to affect the other timers.
After returning from the main loop, every timer is checked to see if it's still alive and, in that case, deleted, before finalizing the library. This is not really necessary, since Ecore Shutdown function will already delete them for you, but it's good practice if you have other things going on after this point that could restart the main loop.