A program (a running Task) can create a new Task by using the spawning expression. It is specified using the spawn keyword followed by a name of an entry code section. A new Task is created and it starts to run concurrently with the Task that used the spawning expression, executing the specified entry code section. The entry code section name can be followed with an expression enclosed in parentheses. If specified, the copy of the expression value is assigned to the startParameter element of the Vars() dictionary (global variables) in the new Task. The current Task handle is assigned to the parent element of the Vars() dictionary in the new Task. The spawning expression value is a Task handle for the newly created Task, or null if the system failed to create a new Task.
In the following example, a program executing the Main entry code section creates a new task that starts to execute the DoBackup entry code section, which copies files "file1","file2",...."file100" into "backup1","backup2",..., files.
entry DoBackup is nameIndex = 1; while nameIndex <= 100 loop fileData = ReadStorageFile("file" + String(nameIndex)); if fileData != null then resultCode = WriteStorageFile("backup" + String(nameIndex),fileData); if resultCode != null then Log("failed to backup file" + String(nameIndex) + ". Error Code=" + resultCode); end if; end if; end loop; end entry; entry Main is backuper = spawn DoBackup; if backuper == null then Log("Failed to start a Backup Task"); end if; end entry;
Tasks do not share any variables, even when a Task directly creates a new Task using the spawning expression.