STARTTASK

Statement

Syntax

Starts a label as a cooperative task that runs alongside the main program, and stores its handle in a variable.

<handle> AS TASKHANDLE
<handle> AS JOB
STARTTASK <handle>, <label>
<label>:
  <statements>
RETURN <value>

Parts

<handle>A plain variable that receives the task handle. Declared AS TASKHANDLE or AS JOB (the same type), it starts as NULL and accepts only tasks.
<label>The name of a label in the program. It must exist: "Unknown BASIC task label".
<statements>The work of the task. It shares the program's variables.
<value>The result of the task, read later with TaskResult().

Good to know

  • The task does not start on the spot. It begins when the main program next yields: Sync(), Wait(), AWAITTASK…
  • Tasks are cooperative: only one piece of code runs at a time, and they take turns only at a yield.
  • The handle must be a plain variable. STARTTASK jobs[1], Worker compiles but the program cannot start. Start into a variable, then copy it.
  • STARTTASK creates the variable if needed, even under OPTION EXPLICIT. Assigning anything but a task to a typed handle stops the program with BASIC value is not a task handle.
  • A bare RETURN leaves no usable result. An error in a task nobody awaits is silent: only TaskFailed() shows it.
  • STARTTASK is allowed inside a FUNCTION. Inside the label, give each yield its own line, with no comment after it (see YieldTask()).

Example

loader AS TASKHANDLESTARTTASK loader, LoadLevelConsole("started")                             ' startedDO  IF TaskDone(loader)    Console("rooms: " + Str(TaskResult(loader)))   ' rooms: 3    EXIT  ENDIF  Sync()LOOPLoadLevel:  FOR room = 1 TO 3    ' one room per frame    Sync()  NEXTRETURN 3
In the manual : The game loop and tasks

Commands that go with it