Get the progress of an Advantage Database query in Delphi
-
24-06-2021 - |
Question
I've searched and searched, but can't find out how to get the progress of a query in Delphi. I've found some information for the Advantage .NET data provider but could use some help with Delphi.
Solution
You want the AdsRegisterCallbackFunction method. Here's a quick example I use for displaying progress during creating indexes for a TAdsTable; it works exactly the same way for progress in a TAdsQuery
:
implementation
var
// Holder for reference to progressbar on form, so it can be
// accessed easily from the callback - see below
PB: TProgressBar = nil;
// replacement for Application.ProcessMessages, since we don't have
// access to the Application from the callback either
procedure KeepWindowsAlive;
var
M: TMsg;
begin
if PeekMessage(M, 0, 0, 0, pm_Remove) then
begin
TranslateMessage(M);
DispatchMessage(M);
end;
end;
// The callback function itself - note the stdcall at the end
// This updates the progressbar with the reported percentage of progress
function ProgressCallback(Percent: Word; CallBackID: LongInt): LongInt; stdcall;
begin
if PB <> nil then
PB.Position := Percent;
KeepWindowsAlive;
Result := 0;
end;
// The button click handler. It registers the callback, calls a function
// that creates the index (not shown here), and then unregisters the callback.
//
// As I mentioned above, it works the same way with a TAdsQuery.
// You'd simply assign the SQL, set any params, and register the
// callback the same way. Then, instead of calling my CreateIndexes
// method, you'd Open the query; it will call the progress callback
// periodically, and when the query finishes you just unhook the
// callback as shown here.
procedure TCreateIndexesForm.CreateButtonClick(Sender: TObject);
begin
// Grab a reference to the progress bar into the unit global, so we don't
// need a reference to the form by name in the callback.
PB := Self.ProgressBar1;
// AdsTable is a property of the form itself. It's set
// during the constructor. It's just a `TAdsTable` instance.
// The index info is set in that constructor as well (tag,
// expression, type, etc.).
AdsTable.AdsRegisterCallbackFunction(@ProgressCallBack, 1);
try
CreateIndexes;
finally
// Unhook the progress callback
AdsTable.AdsClearCallbackFunction;
// Clear the outside reference to the progress bar
PB := nil;
end;
end;
Note that the callback must be a stand-alone procedure (as shown above), not a form method. I've shown a way to not have to hard-code access to a particular form name by using the unit global reference to the progress bar.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow