36 of 286
22 CallTask Type Special Purpose Invocation CallTask taskName, taskStack CallTask taskName, taskStack, taskStackSize CallTask taskName( parameterList ), taskStack CallTask taskName( parameterList ), taskStack, taskStackSize Parameter Method Type Description taskName ByVal identifier The name of the task to invoke. parameterList varies varies Zero or more parameters to be passed to the task, separated by commas. taskStack ByRef array of Byte The stack for the task (see discussion) taskStackSize ByVal integral The size of the stack. Discussion This construct is used to prepare a task for running; the task doesn’t actually execute until its turn comes up in the normal task rotation. In the first and second cases, the taskName given must be the name of a user-defined subroutine that takes no parameters. In the third and fourth cases, the taskName given must be a user-defined subroutine that takes a number of parameters whose type and number match that of the supplied parameter list. The subroutine may be public or private but if it is private it must exist in the same module as the CallTask invocation that refers to it. The taskStack may be a Byte array, typically defined at the module level, that contains a sufficient amount of space for the task’s stack needs. The array can be public or private but if it is private it must exist in the same module as the CallTask invocation that refers to it. Alternately, the stack for a task may be specified by giving its address as an integral expression. In this case, it is usually also advisable to specify the size of the stack since the compiler cannot deduce the size. A task must have exclusive use of the memory dedicated to its task stack. A particular task stack may be used by more than one task but one task must terminate before the next task can re-use the task stack. If a task is passed parameters when it is invoked, it is advisable that those parameters be passed ByVal because the lifetime of the task may exceed the lifetime of the routine from which the task was invoked. If parameters are passed ByRef (explicitly or implicitly), the compiler will issue a warning. Also, certain types of expressions (notably, those involving user-defined functions that return String types) may not be used as parameter values for task invocation because they require the creation of temporary variable space on the stack during evaluation. The compiler will issue an error message when it detects such situations. This problem can be rectified by manually creating a variable (preferably at the module level) to hold the parameter value. For native mode devices (e.g. ZX-24n), the task stack size must either be explicitly specified or it must be determinable by the compiler from the size of the task stack array. The compiler will issue an error message if it cannot determine the size of the task stack. Please read the section on multi-tasking in the ZBasic Reference Manual for more details, including information about how to determine the proper task stack size. Example 1 Dim taskStack(1 to 50) as Byte Sub Main() CallTask MyTask, taskStack Do Debug.Print "Hello from Main" Call Delay(1.0)