'At which moment to update UI when using TADOStoredProc with eoAsyncExecute and eoAsyncFetch

My Delphi 6 application had a TADOStoredProc that executed a time consuming query. I switched it to asynchronous mode with [eoAsyncExecute, eoAsyncFetch, eoAsyncFetchNonBlocking] in order to make the application look as if it responds quicker.

At the end of execution I need to signalize to the UI that it can update. Updating is sometimes a heavy operation. If it is performed in OnFetchComplete, it may cause DB Grid not to display all the results. I found temporarily no better alternative than to update the UI in a timer that fires once and is triggered from OnFetchComplete. This time it seems to work and no data seems to be missing.

Please give a clue how to trigger UI update in a proper way.

ListColResizeTimer: TTimer;
dg: TAutoSizeTntDBGrid; //Derived from TTntDBGrid
sp: TADOStoredProc;

sp.ExecuteOptions := [eoAsyncExecute, eoAsyncFetch, eoAsyncFetchNonBlocking];
sp.OnFetchComplete := _spFetchComplete;
Screen.Cursor := crSQLWait;
dg.Enabled := False;
TItems.List(sp, StartOfTheDay(ADate), EndOfTheDay(ADate));

procedure TItemsFrame._spFetchComplete(DataSet: TCustomADODataSet;
  const Error: Error; var EventStatus: TEventStatus);
begin
  try
    Screen.Cursor := crDefault;
    dg.Enabled := True;
    //If I call dg.AutoSizeColumns here, the grid contents may show not all data
    //dg.AutoSizeColumns uses DataLink property of DBGrid
    //I found no better alternative than launching a 200ms timer to fire dg.AutoSizeColumns
    ListColResizeTimer.Enabled := False;
    ListColResizeTimer.Enabled := True;
  except
  end;
end;

procedure TItemsFrame.ListColResizeTimerTimer(Sender: TObject);
begin
  TTimer(Sender).Enabled := False;
  dg.AutoSizeColumns(); // Can be time consuming
  Application.ProcessMessages(); //Trying to make Screen.Cursor really update to the normal
end;


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source