IDalCommand2.CreateTask

Syntax

CreateTask(ExecuteType: DalCommandExecuteType): 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.

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.

See also:

IDalCommand2