Creating and handling Tasks

This article explains how to create and handle Tasks via TwinCAT Automation Interface. It consists of the following topics:

General Information

Two task types can be configured in TwinCAT XAE and therefore also with the Automation Interface: tasks with and without a process image. When you insert a new task in TwinCAT XAE, you can decide whether you want to insert a process image or not by selecting or deselecting the corresponding checkbox in the "Insert Task" dialog box.

Creating and handling Tasks 1:

The inserted task then either contains three further subordinate nodes (image, inputs, outputs) or not - as shown in the following example (task 1 = with image, task 2 = without image).

Creating and handling Tasks 2:

Inserting Tasks

To insert a Task via Automation Interface, you can make use of the ITcSmTreeItem::CreateChild() method and the corresponding SubTypes for "With Image" (SubType = 0) and "Without Image" (SubType = 1).

Code Snippet (C#)

ITcSmTreeItem tasks = systemManager.LookupTreeItem("TIRT");
tasks.CreateChild("Task 1 (With Image)", 0, null, null);

Code Snippet (Powershell):

$tasks = $systemManager.LookupTreeItem("TIRT")
$tasks.CreateChild("Task 1 (With Image)", 0, $null, $null)

Code Snippet (C#)

ITcSmTreeItem tasks = systemManager.LookupTreeItem("TIRT");
tasks.CreateChild("Task 2 (Without Image)", 1, null, null);

Code Snippet (Powershell):

$tasks = $systemManager.LookupTreeItem("TIRT")
$tasks.CreateChild("Task 1 (Without Image)", 1, $null, $null)

Inserting input/output variables

Input and output variables can be added to process images (task "With Image"). These can then be linked with the various I/O devices or variables of other tasks. You can use the corresponding TwinCAT XAE dialog to select the data type and the address of the input/output variables in the process image, for example. Click on "Ok" to add the variable to the process image.

Creating and handling Tasks 3:

This procedure can also be triggered via the Automation Interface using the ITcSmTreeItem::CreateChild() method with the corresponding variable data type as vInfo. In this case, SubType specifies the "Start Address" as shown in the dialog above.

Code snippet (C#):

ITcSmTreeItem task1 = systemManager.LookupTreeItem("TIRT^Task 1 (With Image)^Inputs");
task1.CreateChild("bInput", -1, null, "BOOL");

Code snippet (PowerShell):

$task1 = $systemManager.LookupTreeItem("TIRT^Task 1 (With Image)^Inputs")
$task1.CreateChild("bInput", -1, $null, "BOOL")

If SubType = -1, TwinCAT automatically appends the new variable to the end of the variable list.