Executing SQL Queries in Asynchronous Mode

Asynchronous execution enables the user to continue working in the application without waiting for result of the SQL query execution. To do this, create a specialized connection in advance. Within this connection, create a command and set its parameters using the IDalConnection.CreateCommand method. Execution of commands using the IDalCommand.Execute or IDalCommand.ExecuteWithoutLast methods is not supported within the specialized connection. Commands are executed as separate tasks. To work with tasks in the Fore language, use properties and methods of the ITask interface. The task of executing SQL queries in the asynchronous mode is described in the IDalCommandTask interface. To create a task, cast the created command to the IDalCommand2 type and call the IDalCommand2.CreateTask method:

Var
    //...
    ORCLAsyncConnect: IDalConnection;
    Command: IDalCommand;
    CmdParams: IDalCommandParams;
    Param: IDalCommandParam;
    TaskCommand: IDalCommandTask;
    //...   
Begin
    //...
    Command := ORCLAsyncConnect.CreateCommand;
    Command.SQL := "insert into Table1(Code) values(:ParamArray)";
    Command.Parse;
    CmdParams := Command.Params;
    CmdParams.Item(0).DataType := DbDataType.Integer;
    Command.MaxParamsRows := 10000;
    //Set parameter values
    //...
    //Create an SQL query execution task in asynchronous mode
    TaskCommand := (Command As IDalCommand2).CreateTask(DalCommandExecuteType.Execute);

To execute the task, cast it to the ITask type and call the ITask.Start method.

Var
    //...
    TaskCommand: IDalCommandTask;
    //...
Begin
    //...
    (TaskCommand As ITask).Start;

After this the SQL query is executed in parallel to the current application. The ITask.State property can be used to track task status. The SQL query execution result can be obtained in the IDalCommandTask.Result property.

See also:

Introduction