Syntax
Asks a task to stop. The task stops at its next yield, and the code after that yield never runs.
CANCELTASK <handle>Parts
| <handle> | A task handle: a variable filled by STARTTASK. |
|---|
Good to know
- The yields are Wait(), Sleep(), Sync(), Swap() and
YieldTask(). A task that never yields cannot be stopped: it runs to itsRETURN. TaskCancelled()is true at once.AWAITTASKon the task then returns without an error, andTaskResult()raises an error.- On a task that already ended,
CANCELTASKdoes nothing. - A task cancelled right after
STARTTASKstill runs up to its first yield. - A
TRYin the task around a yield catches the cancellation (BASIC task cancelled), and the task goes on to its next yield. AddRETHROWin that CATCH. - Allowed inside a
FUNCTION.
Example
steps = 0walker AS TASKHANDLESTARTTASK walker, WalkWait(0.1)CANCELTASK walkerAWAITTASK walkerIF TaskCancelled(walker) THEN Console("walker cancelled") ' walker cancelledIF steps < 1000 THEN Console("stopped early") ' stopped earlyENDWalk: FOR i = 1 TO 1000 steps = steps + 1 YieldTask() NEXTRETURN stepsIn the manual : The game loop and tasks