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 used to work with the database using SQL queries in the asynchronous mode.

Comments

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

Fore Example

Executing the example requires a form and six buttons positioned on it. The first three buttons are used to create and launch the task, the other ones are used to obtain 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 on the server. A large number of records is supposed to be in the table. 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;
    
    //Task of the data extraction
    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;
        //The check if connection can work in the asynchronous mode
        If SC.Type <> DalConnectionType.Async Then
            SCAsync := SC.Clone(DalConnectionType.Async);
        End If;
        //Command of the data extraction
        Command := SCAsync.CreateCommand("Select * From Table1");
        //Creation of  the data extraction task
        DalTask := (Command As IDalCommand2).CreateTask(DalCommandExecuteType.CreateCursor);
        Task1 := DalTask As ITask;
        Task1.Start;
    End Sub Button1OnClick;
    
    //Task of changing the data
    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;
        //The check if connection can work in the asynchronous mode
        If SC.Type <> DalConnectionType.Async Then
            SCAsync := SC.Clone(DalConnectionType.Async);
        End If;
        //Command of changing the data
        Command := SCAsync.CreateCommand("Insert Into Table1...");
        //Creation of the task for changing the data
        DalTask := (Command As IDalCommand2).CreateTask(DalCommandExecuteType.Execute);
        Task2 := DalTask As ITask;
        Task2.Start;
    End Sub Button2OnClick;
    
    //Task of executing the procedure stored on 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;
        //The check if connection can work in the asynchronous mode
        If SC.Type <> DalConnectionType.Async Then
            SCAsync := SC.Clone(DalConnectionType.Async);
        End If;
        //Command of changing the data
        Command := SCAsync.CreateCommand("");
        Command.SQL := "USERSTOREDPROCEDURE";
        Command.Type := DalCommandType.StoredProcedure;
        Param := Command.Params.Add("ITEM");
        Param.Direction := DalParamDirection.Output;
        Param.DataType := DbDataType.Integer;
        //Creation of the task for changing the data
        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 the execution of stored procedure
                    Params := Result.Params;
                    //...
                End If;
            Else
                //Resultant cursor with data
                Cur := Result.CreateCursor;
                //...
            End If;
        End If;
    End Sub TaskResult;

End Class TESTForm;

After launching the form, if 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 obtain the appropriate result.

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

Fore.NET example

Executing the example requires the .NET form and six buttons positioned on it. The first three buttons are used to create and launch the task, the other ones are used to obtain 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 should be created in the database on the server. A large number of records is supposed to be in the table. 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;

    //Task of the data extraction
    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;
        //The check if connection can work in the asynchronous mode
        If SC.Type <> DalConnectionType.dctAsync Then
            SCAsync := SC.Clone(DalConnectionType.dctAsync);
        End If;
        //Command of the data extraction
        Command := SCAsync.CreateCommand("Select * From Table1");
        //Creation of  the data extraction task
        DalTask := (Command As IDalCommand2).CreateTask(DalCommandExecuteType.daextyCreateCursor);
        Task1 := DalTask As ITask;
        Task1.Start();
    End Sub;
    
    //Task of changing the data
    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;
        //The check if connection can work in the asynchronous mode
        If SC.Type <> DalConnectionType.dctAsync Then
            SCAsync := SC.Clone(DalConnectionType.dctAsync);
        End If;
        //Command of changing the data
        Command := SCAsync.CreateCommand("Insert Into Table1...");
        //Creation of the task for changing the data
        DalTask := (Command As IDalCommand2).CreateTask(DalCommandExecuteType.daextyExecute);
        Task2 := DalTask As ITask;
        Task2.Start();
    End Sub;

    //Task of executing the procedure stored on 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;
        //The check if connection can work in the asynchronous mode
        If SC.Type <> DalConnectionType.dctAsync Then
            SCAsync := SC.Clone(DalConnectionType.dctAsync);
        End If;
        //Command of changing the data
        Command := SCAsync.CreateCommand("");
        Command.SQL := "USERSTOREDPROCEDURE";
        Command.Type := DalCommandType.dctStoredProcedure;
        Param := Command.Params.Add("ITEM");
        Param.Direction := DalParamDirection.dpdOutput;
        Param.DataType := DbDataType.ddtInteger;
        //Creation of the task for changing the data
        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 launching the form, if 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 obtain the appropriate result.

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

See also:

IDalCommand2