delphidelphi-xe2anydac

How to keep a program responsive while executing Anydac Query?


With sdac I can let the program responsive for user input as :

while Query.Executing do
begin
  Application.ProcessMessages;
  Sleep(1);
end;

How do I implement the same code with anydac query (there is no similar function)?

I'm using delphi xe2 and anydac v6.


Solution

  • AnyDAC supports different execution modes. To check the current operation status use ADQuery1.Command.State. This is pseudo-code (I don't have Delphi here):

    ADQuery1.ResourceOptions.CmdExecMode := amAsync;
    ADQuery1.Open;
    while ADQuery1.Command.State = csExecuting do
    begin
        // This is NOT RECOMMENDED
        Application.ProcessMessages;
        Sleep(1);
    end;
    

    However, since the only thing you are doing in your while block is processing GUI messages, I think your best bet is using amNonBlocking mode, which will wait for the query to finish (thus avoiding the while block) but doesn't block the GUI (it does ignore keyboard and mouse events, thought.)

    ADQuery1.ResourceOptions.CmdExecMode := amNonBlocking;
    ADQuery1.Open;
    

    For more information, see the documentation in the developer's website: Asynchronous Execution.