Search code examples
delphidbexpress

How do I keep my program responsive to user input while executing a DBExpress query?


ibdac query ( http://www.devart.com/ibdac/components.html ) has a function executing where I can write something like:

 while MyQuery.Executing do
 begin
   application.ProcessMessages;
   Sleep(1);
 end;

how do I implement the same code with a dbexpress query (there is no similar function)?


Solution

  • There is no similar functionality. But you can execute MyQuery in a background thread and main thread will wait when the background thread is finished. For example:

    type
      TMyThread = class(TThread)
      private
        FQuery: TSQLQuery;
      protected
        procedure Execute; override;
      public
        constructor Create(AQuery: TSQLQuery);
      end;
    
    constructor TMyThread.Create(AQuery: TSQLQuery);
    begin
      inherited Create;
      FreeOnTerminate := False;
      FQuery := AQuery;
    end;
    
    procedure TMyThread.Execute;
    begin
      FQuery.ExecSQL;
    end;
    
    var
      oThread: TMyThread;
    ....
    
      oThread := TMyThread.Create(MyQuery);
      try
        while not oThread.Finished do begin
          Application.ProcessMessages;
          Sleep(1);
        end;
      finally
        oThread.Free;
      end;
    

    PS: Btw, i am using AnyDAC. It has build-in background execution.