IDalCommand2.CreateTask

Fore Syntax

CreateTask(ExecuteType: DalCommandExecuteType): IDalCommandTask;

Fore.NET Syntax

CreateTask(ExecuteType: Prognoz.Platform.Interop.Dal.DalCommandExecuteType): Prognoz.Platform.Interop.Dal.IDalCommandTask;

Parameters

ExecuteType. Task purpose.

Description

The CreateTask method creates a new task that is used to work with database using SQL queries in the asynchronous mode.

Comments

To control the task, cast the created object to the ITask type.

Fore Example

Executing the example requires a form and six buttons on it. The first three buttons are used to create and start the task, the other ones are used to get the result of tasks execution. The repository contains a connection with a database with the BD identifier. The table with the Table1 physical name and the USERSTOREDPROCEDURE procedure must be created in the database at the server. The table is supposed to have a number of records. The procedure executes any continuous action and has one output parameter that takes the integer value.

Class TESTForm: Form
    Button1: Button;
    Button2: Button;
    Button3: Button;
    Button4: Button;
    Button5: Button;
    Button6: Button;
    DalTask1, DalTask2, DalTask3: IDalCommandTask;
    Task1, Task2, Task3: ITask;
    
    //Data extraction task
    Sub Button1OnClick(Sender: Object; Args: IMouseEventArgs);
    Var
        MB: IMetabase;
        DBInst: IDatabaseInstance;
        SC: ISecurityConnection2;
        SCAsync: ISecurityConnection;
        Command: IDalCommand;
        DalTask: IDalCommandTask;
    Begin
        MB := MetabaseClass.Active;
        DBInst := MB.ItemById("BD").Open(NullAs IDatabaseInstance;
        SC := DBInst.Connection As ISecurityConnection2;
        //Check if connection can work in asynchronous mode
        If SC.Type <> DalConnectionType.Async Then
            SCAsync := SC.Clone(DalConnectionType.Async);
        End If;
        //Data extraction command
        Command := SCAsync.CreateCommand("Select * From Table1");
        //Create a data extraction task
        DalTask := (Command As IDalCommand2).CreateTask(DalCommandExecuteType.CreateCursor);
        Task1 := DalTask As ITask;
        Task1.Start;
    End Sub Button1OnClick;
    
    //Data change task
    Sub Button2OnClick(Sender: Object; Args: IMouseEventArgs);
    Var
        MB: IMetabase;
        DBInst: IDatabaseInstance;
        SC: ISecurityConnection2;
        SCAsync: ISecurityConnection;
        Command: IDalCommand;
        DalTask: IDalCommandTask;
    Begin
        MB := MetabaseClass.Active;
        DBInst := MB.ItemById("BD").Open(NullAs IDatabaseInstance;
        SC := DBInst.Connection As ISecurityConnection2;
        //Check if connection can work in asynchronous mode
        If SC.Type <> DalConnectionType.Async Then
            SCAsync := SC.Clone(DalConnectionType.Async);
        End If;
        //Data change command
        Command := SCAsync.CreateCommand("Insert Into Table1...");
        //Create a data change task
        DalTask := (Command As IDalCommand2).CreateTask(DalCommandExecuteType.Execute);
        Task2 := DalTask As ITask;
        Task2.Start;
    End Sub Button2OnClick;
    
    //Task for executing procedure stored at server
    Sub Button3OnClick(Sender: Object; Args: IMouseEventArgs);
    Var
        MB: IMetabase;
        DBInst: IDatabaseInstance;
        SC: ISecurityConnection2;
        SCAsync: ISecurityConnection;
        Command: IDalCommand;
        Param: IDalCommandParam;
        DalTask: IDalCommandTask;
    Begin
        MB := MetabaseClass.Active;
        DBInst := MB.ItemById("BD").Open(NullAs IDatabaseInstance;
        SC := DBInst.Connection As ISecurityConnection2;
        //Check if connection can work in asynchronous mode
        If SC.Type <> DalConnectionType.Async Then
            SCAsync := SC.Clone(DalConnectionType.Async);
        End If;
        //Data change command
        Command := SCAsync.CreateCommand("");
        Command.SQL := "USERSTOREDPROCEDURE";
        Command.Type := DalCommandType.StoredProcedure;
        Param := Command.Params.Add("ITEM");
        Param.Direction := DalParamDirection.Output;
        Param.DataType := DbDataType.Integer;
        //Create a data change task
        DalTask := (Command As IDalCommand2).CreateTask(DalCommandExecuteType.Execute);
        Task3 := DalTask As ITask;
        Task3.Start;
    End Sub Button3OnClick;

    Sub Button4OnClick(Sender: Object; Args: IMouseEventArgs);
    Begin
        If Not IsNull(Task1) Then
            TaskResult(Task1);
        End If;
    End Sub Button4OnClick;

    Sub Button5OnClick(Sender: Object; Args: IMouseEventArgs);
    Begin
        If Not IsNull(Task2) Then
            TaskResult(Task2);
        End If;
    End Sub Button5OnClick;

    Sub Button6OnClick(Sender: Object; Args: IMouseEventArgs);
    Begin
        If Not IsNull(Task3) Then
            TaskResult(Task3);
        End If;
    End Sub Button6OnClick;
    
    Sub TaskResult(Task: ITask);
    Var
        DalTask: IDalCommandTask;
        Result: IDalCommand;
        Count: Integer;
        Params: IDalCommandParams;
        Cur: IDalCursor;
    Begin
        If Task.IsCompleted And (Task.State = TaskState.RanToCompletion) Then
            DalTask := Task As IDalCommandTask;
            Result := DalTask.Result;
            If DalTask.ExecuteType = DalCommandExecuteType.Execute Then
                If Result.Type <> DalCommandType.StoredProcedure Then
                    //Number of processed records
                    Count := Result.Execute;
                    //...
                Else
                    //Parameters that contain a result of execution of stored procedure
                    Params := Result.Params;
                    //...
                End If;
            Else
                //Output cursor with data
                Cur := Result.CreateCursor;
                //...
            End If;
        End If;
    End Sub TaskResult;

End Class TESTForm;

After starting the form, if the user presses one of first three buttons, the task that executes the SQL query to the database is started. The task is executed asynchronously relative to the work of the form. The result of the task work can be obtained by pressing the fourth-sixth buttons. Depending on the task that is executed the result is different. The TaskResult procedure is used to check the type of the task and to get the appropriate result.

The TaskResult procedure works if the task was executed without errors. If required, the code of this procedure can be changed to check the other states of the tasks.

Fore.NET example

Executing the example requires a .NET form and six buttons on it. The first three buttons are used to create and start the task, the other ones are used to get the result of tasks execution. The repository contains a connection with a database with the BD identifier. The table with the Table1 physical name and the USERSTOREDPROCEDURE procedure must be created in the database at the server. The table is supposed to have a number of records. The procedure executes any continuous action and has one output parameter that takes the integer value.

Imports Prognoz.Platform.Forms.Net;
Imports Prognoz.Platform.Interop.Dal;
Imports Prognoz.Platform.Interop.Db;
Imports Prognoz.Platform.Interop.ForeSystem;
Imports Prognoz.Platform.Interop.Metabase;

Public Partial Class TESTForm: Prognoz.Platform.Forms.Net.ForeNetForm
    Public Constructor TESTForm();
    Begin
        InitializeComponent();
    End Constructor;
    
    DalTask1: IDalCommandTask;
    DalTask2: IDalCommandTask;
    DalTask3: IDalCommandTask;
    Task1: ITask;
    Task2: ITask;
    Task3: ITask;

    //Data extraction task
    Private Sub button1_Click(sender: System.Object; e: System.EventArgs);
    Var
        MB: IMetabase;
        DBInst: IDatabaseInstance;
        SC: ISecurityConnection2;
        SCAsync: ISecurityConnection;
        Command: IDalCommand;
        DalTask: IDalCommandTask;
    Begin
        MB := Self.Metabase;
        DBInst := MB.ItemById["BD"].Open(NullAs IDatabaseInstance;
        SC := DBInst.Connection As ISecurityConnection2;
        //Check if connection can work in asynchronous mode
        If SC.Type <> DalConnectionType.dctAsync Then
            SCAsync := SC.Clone(DalConnectionType.dctAsync);
        End If;
        //Data extraction command
        Command := SCAsync.CreateCommand("Select * From Table1");
        //Create a data extraction task
        DalTask := (Command As IDalCommand2).CreateTask(DalCommandExecuteType.daextyCreateCursor);
        Task1 := DalTask As ITask;
        Task1.Start();
    End Sub;
    
    //Data change task
    Private Sub button2_Click(sender: System.Object; e: System.EventArgs);
    Var
        MB: IMetabase;
        DBInst: IDatabaseInstance;
        SC: ISecurityConnection2;
        SCAsync: ISecurityConnection;
        Command: IDalCommand;
        DalTask: IDalCommandTask;
    Begin
        MB := Self.Metabase;
        DBInst := MB.ItemById["BD"].Open(NullAs IDatabaseInstance;
        SC := DBInst.Connection As ISecurityConnection2;
        //Check if connection can work in asynchronous mode
        If SC.Type <> DalConnectionType.dctAsync Then
            SCAsync := SC.Clone(DalConnectionType.dctAsync);
        End If;
        //Data change task
        Command := SCAsync.CreateCommand("Insert Into Table1...");
        //Create a data change task
        DalTask := (Command As IDalCommand2).CreateTask(DalCommandExecuteType.daextyExecute);
        Task2 := DalTask As ITask;
        Task2.Start();
    End Sub;

    //Task of executing procedure stored at server
    Private Sub button3_Click(sender: System.Object; e: System.EventArgs);
    Var
        MB: IMetabase;
        DBInst: IDatabaseInstance;
        SC: ISecurityConnection2;
        SCAsync: ISecurityConnection;
        Command: IDalCommand;
        Param: IDalCommandParam;
        DalTask: IDalCommandTask;
    Begin
        MB := Self.Metabase;
        DBInst := MB.ItemById["BD"].Open(NullAs IDatabaseInstance;
        SC := DBInst.Connection As ISecurityConnection2;
        //Check if connection can work in asynchronous mode
        If SC.Type <> DalConnectionType.dctAsync Then
            SCAsync := SC.Clone(DalConnectionType.dctAsync);
        End If;
        //Data change command
        Command := SCAsync.CreateCommand("");
        Command.SQL := "USERSTOREDPROCEDURE";
        Command.Type := DalCommandType.dctStoredProcedure;
        Param := Command.Params.Add("ITEM");
        Param.Direction := DalParamDirection.dpdOutput;
        Param.DataType := DbDataType.ddtInteger;
        //Create a data change task
        DalTask := (Command As IDalCommand2).CreateTask(DalCommandExecuteType.daextyExecute);
        Task3 := DalTask As ITask;
        Task3.Start();
    End Sub;

    Private Sub button4_Click(sender: System.Object; e: System.EventArgs);
    Begin
        If Task1 <> Null Then
            TaskResult(Task1);
        End If;
    End Sub;

    Private Sub button5_Click(sender: System.Object; e: System.EventArgs);
    Begin
        If Task2 <> Null Then
            TaskResult(Task2);
        End If;
    End Sub;

    Private Sub button6_Click(sender: System.Object; e: System.EventArgs);
    Begin
        If Task3 <> Null Then
            TaskResult(Task3);
        End If;
    End Sub;

    Sub TaskResult(Task: ITask);
    Var
        DalTask: IDalCommandTask;
        Result: IDalCommand;
        Count: Integer;
        Params: IDalCommandParams;
        Cur: IDalCursor;
    Begin
        If Task.IsCompleted And (Task.State = TaskState.atsRanToCompletion) Then
            DalTask := Task As IDalCommandTask;
            Result := DalTask.Result;
            If DalTask.ExecuteType = DalCommandExecuteType.daextyExecute Then
                If Result.Type <> DalCommandType.dctStoredProcedure Then
                    //Number of processed records
                    Count := Result.Execute();
                    //...
                Else
                    //Parameters containing result of stored operation execution
                    Params := Result.Params;
                    //...
                End If;
            Else
                //Resulting cursor with data
                Cur := Result.CreateCursor();
                //...
            End If;
        End If;
    End Sub;
    
End Class;

After starting the form, if the user presses one of first three buttons, the task that executes the SQL query to the database is started. The task is executed asynchronously relative to the work of the form. The result of the task work can be obtained by pressing the fourth-sixth buttons. Depending on the task that is executed the result is different. The TaskResult procedure is used to check the type of the task and to get the appropriate result.

The TaskResult procedure works if the task was executed without errors. If required, the code of this procedure can be changed to check the other states of the tasks.

See also:

IDalCommand2