3

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)?

Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467
ae1080
  • 374
  • 2
  • 7
  • 14

1 Answers1

5

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.

oodesigner
  • 1,007
  • 6
  • 8
  • 2
    if the Query runs in a thread, no ProcessMessage is required to keep the user interface responsive... – mjn Sep 19 '11 at 11:00
  • 4
    as SQL connections are not threadsafe, the Query thread should also receive its own connection or connection factory object – mjn Sep 19 '11 at 11:01
  • @mjn 1) Actually no other need for ProcessMessage with ibdac, than to wait for the query finish and do some other things in the loop. 2) Agree about thread safety, but my intent was to show the general approach. Also, if the MyQuery processing will be serialized by the application, then no need for "its own connection". So, that depends ... – oodesigner Sep 19 '11 at 11:21
  • 3
    Actually, this while loop would be best of all with `MsgWaitForMultipleObjects` so that you don't have to wake up every milli-second to poll the flag. I hate polling as I think regular visitors will not be well aware. And that way avoids the need for a Finished flag, just wait until the thread is signaled, but wake up if an message arrives in the queue and process it. – David Heffernan Sep 19 '11 at 11:37