MainLoopScheduler
¶
MainLoopScheduler implements the Kanzi application main loop as a sequence of stages, each consisting of a sequence of tasks, where a task is a callable method.
You can customize the application main loop by extending its stages with additional tasks, replacing existing tasks with your own implementations, or adding timer tasks to execute some functionality at regular intervals.
To register a task, call a prepend or append function.
Specifically, those functions are MainLoopScheduler.prependTask and MainLoopScheduler.appendTask.
-- Define the closure which you want to register
local handler = function()
print("Task is executed")
end
-- Register it in the main loop scheduler.
local token = MainLoopScheduler.prependTask(
MainLoopStage.USER,
"MyTask",
TaskRecurrence.Recurring,
handler
)
Remove the task through MainLoopScheduler.removeTask and MainLoopScheduler.clearTasks functions.
-- Remove previously registered task by passing in its stage and a token.
MainLoopScheduler.removeTask(MainLoopStage.USER, token)
token = nil
-- You can also remove all tasks on a stage with a single call
MainLoopScheduler.clearTasks(MainLoopStage.USER)
Registering timers follows similar API to registering tasks.
The main difference is that you need to provide an interval after which the task will be executed.
The interval should be specified in nanoseconds.
Helper functions utils.secs, utils.millis, utils.micros and utils.nanos can be used to make the intent clearer.
-- Define the closure which you want to register
local handler = function()
print("Timer is fired")
end
-- Register the timer in the main loop scheduler to run every 5 ms.
local token = MainLoopScheduler.prependTimer(
MainLoopStage.USER,
"MyTimer",
TimerRecurrence.Recurring,
secs(5),
handler
)
Removing timers also follows similar API to tasks. However note, that the stage does not need to be passed.
-- Remove previously registered task by passing in its stage and a token.
MainLoopScheduler.removeTimer(token)
token = nil
| prependTask() | Adds a task to the front of a stage |
| appendTask() | Adds a task to the back of a stage |
| replaceTask() | Replaces a task in a stage with another one |
| removeTask() | Removes a task from a stage |
| clearTasks() | Removes all tasks in a stage |
| prependTimer() | Adds a timer task to be executed before a stage in the main loop |
| appendTimer() | Adds a timer task to be executed after a stage in the main loop |
| removeTimer() | Removes a timer task from the main loop |
| setCurrentFrameRendered() | Sets whether rendering was performed in the current frame |
| isCurrentFrameRendered() | Returns whether rendering was performed in the current frame |
| setFrameRateLimit() | Sets the frame rate limit of the application |
| enableSuspendWhenIdle() | Sets whether an application allows suspension |
| isSuspendWhenIdleEnabled() | Returns whether an application allows suspension |
Adds a task to the front of a stage.
| stage | (MainLoopStage) | Reference to the stage. |
| name | (string) | Name for the task. |
| recurrence | (TaskRecurrence) | Recurrence choice for the task that you want to insert. |
| task | (function) | The task that you want to insert. |
| (MainLoopTaskToken) | Token of the added task. You can use this token to remove or replace the task. |
Adds a task to the back of a stage.
| stage | (MainLoopStage) | Reference to the stage. |
| name | (string) | Name for the task. |
| recurrence | (TaskRecurrence) | Recurrence choice for the task that you want to insert. |
| task | (function) | The task that you want to insert. |
| (MainLoopTaskToken) | Token of the added task. You can use this token to remove or replace the task. |
Replaces a task in a stage with another one.
| stage | (MainLoopStage) | Reference to the stage. |
| token | (MainLoopTaskToken) | Token of the task. |
| task | (function) | The task with which to replace the current task. |
| (boolean) | If replacement is successful, true. If the function cannot find the matching token, false. |
Removes a task from a stage.
| stage | (MainLoopStage) | Reference to the stage. |
| token | (MainLoopTaskToken) | Token of the task. |
| (boolean) | If removal is successful, true. If the function cannot find the matching token, false. |
Removes all tasks in a stage.
| stage | (MainLoopStage) | Reference to the stage. |
Adds a timer task to be executed before a stage in the main loop.
| stage | (MainLoopStage) | Reference to the stage. |
| name | (string) | Name for the task. |
| recurrence | (TimerRecurrence) | Recurrence type of the task. |
| interval | (number) | The length of time in nanoseconds, after which to execute the task. |
| task | (function) | The task. |
| (MainLoopTimerToken) | Token of the added task. You can use this token to remove the task. |
Adds a timer task to be executed after a stage in the main loop.
| stage | (MainLoopStage) | Reference to the stage. |
| name | (string) | Name for the task. |
| recurrence | (TimerRecurrence) | Recurrence type of the task. |
| interval | (number) | The length of time in nanoseconds, after which to execute the task. |
| task | (function) | The task. |
| (MainLoopTimerToken) | Token of the added task. You can use this token to remove the task. |
Removes a timer task from the main loop.
| token | (MainLoopTimerToken) | Token of the task. |
| (boolean) | If removal is successful, true. If the function cannot find the matching token, false. |
Sets whether rendering was performed in the current frame.
Call this function from a task in the Render stage to inform the main loop that the task performed rendering.
The main loop uses this as a precondition for executing the Present stage for a frame.
Returns whether rendering was performed in the current frame.
The main loop uses this as a precondition for executing the Present stage for a frame.
| (boolean) | If rendering was performed in the current frame, true, otherwise false. |
Sets the frame rate limit of the application. This sets an upper limit on the number of frames rendered by Kanzi every second.
| framerate | (number) | The number of frames to render every second. To disable frame rate limit, use 0. |
Sets whether an application allows suspension.
| suspendEnable | (boolean) | If the application allows suspension, use true, otherwise false. |
Returns whether an application allows suspension.
| (boolean) | If the application allows suspension, true, otherwise false. |