Introduction
In CET, task schedulers are the objects which own, manage, and run tasks. They're also responsible for managing priority, and for sleeping and waking tasks. Task schedulers are a part of the main loop of CET.
Scheduler Options
In CET, there are two different task schedulers available for general use. Both schedulers are inside the tasks.cm
file in the cm.task
package.
Scheduler | Description |
---|---|
idleTasks |
The idle task scheduler, which runs tasks while no active Animation is animating. |
priorityTasks |
The priority task scheduler, which should only be used when tasks need to run while an Animation is animating. |
Adding a Task to the Scheduler
The .add(..)
method on task schedulers adds a task to the scheduler. The method takes a descriptive name and a function with the task function signature as required parameters.
idleTasks.add("Name of task", function taskFn);
Parameter | Required | Description |
---|---|---|
str name |
Yes | A descriptive name given to the task. |
taskCB fn |
Yes | A callback function to run for this task. |
Object arg |
No | An object to be passed to the task when it executes. |
int priority |
No | A priority given to this task. |
timespan sleep |
No | Sets the sleep value of the task. |
bool waiting |
No | This task should sleep before executing for the first time. |
bool modal |
No | Allow this task to run while modals are open. |
bool trace |
No | Add task tracing. (for debug use). |
bool uniqueArg |
No | Block this task from the scheduler if the argument isn't unique. |
int overshoot |
No | Intended for internal use. |
bool unpredictable |
No | Not used. |
Task Priority
Tasks inserted into to the scheduler may include a priority in the add(..)
method which informs the scheduler of the tasks' relative importance. A task with too high a priority will cause a task to run ahead of more important tasks. A task with too low of a priority may cause a task to not run as often as needed.
Task Type | Priority Recommendation | Example |
---|---|---|
Tasks whose results aren't relevant nor observable to the user. | 100 through 500 | Cache Maintenance, Server Status Checking |
Tasks whose results are observable to the user. | 10 through 100 | Loading Domains from File, Checking Status of a User Operation |
Task Sleeping
A task which requests to sleep will be ignored by the scheduler and not run for a minimum period of time set by a timespan
in the sleep
parameter on the add(..)
method. This is useful for tasks that need to run periodically, such as a task that checks the status of a network operation. A task never sleeps unless it explicitly requests to do so. Tasks should only sleep for durations greater than one second.
Forcing Task Completion
The finish(..)
methods on the scheduler force a task to complete. This is useful when a user requires a task's results right away. An example of this would be when a user wishes to use data from a file which is being loaded by a task. Upon the users request for the data, the task should be forced to finish.
Tasks may fail to finish if they are incorrectly written, or leave CET unresponsive longer than expected. If a task fails to finish, the finish method will return false
. If a task finishes, the finish method will return true
.
Code Examples
- Adding a task to the scheduler.
idleTasks.add("Example Task", function exampleTask, arg=9999);
- Retrieving a task from the scheduler.
Task task = idleTasks.get("Example Task");
- Finishing a task by name.
bool taskCompleted = idleTasks.finish("Example Task");
- Finishing a task by function.
idleTasks.finish(function exampleTask);
- Finishing a task by function and argument.
idleTasks.finish(function exampleTask, myArg);
Comments
0 comments
Please sign in to leave a comment.