NXC  Version 1.2.1 r5
 All Data Structures Files Functions Variables Groups Pages
Tasks

Since the NXT supports multi-threading, a task in NXC directly corresponds to an NXT thread.

Tasks are defined using the task keyword with the syntax shown in the code sample below.

task name()
{
// the task's code is placed here
}

The name of the task may be any legal identifier. A program must always have at least one task - named "main" - which is started whenever the program is run. The body of a task consists of a list of statements.

You can start and stop tasks with the start and stop statements, which are discussed below. However, the primary mechanism for starting dependant tasks is scheduling them with either the Precedes or the Follows API function.

The StopAllTasks API function stops all currently running tasks. You can also stop all tasks using the Stop function. A task can stop itself via the ExitTo function. Finally, a task will stop itself simply by reaching the end of its body.

In the code sample below, the main task schedules a music task, a movement task, and a controller task before exiting and allowing these three tasks to start executing concurrently. The controller task waits ten seconds before stopping the music task, and then waits another five seconds before stopping all tasks to end the program.

task music() {
while (true) {
}
}
task movement() {
while (true) {
OnFwd(OUT_A, Random(100));
}
}
task controller() {
stop music;
}
task main() {
Precedes(music, movement, controller);
}