Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid of hourglass mouse cursor when executing SQL query with TFDQuery in Delphi XE5

Tags:

delphi

firedac

I have grid control on form and when current record change i need to load RTF stored in DB. It works fine in general, but when i switch records i can see changes of mouse cursor to hourglass and back to regular:

function TComments.GetDocument(AID: integer; ADst: TStream):Boolean;
begin
  try
    SelectQuery.Close;
    SelectQuery.Params.Clear;
    SelectQuery.SQL.Text :=
      'SELECT Dokument from Kommentarer ' +
      'WHERE ID = :ID';
    SelectQuery.ParamByName('ID').AsInteger := AID;
    SelectQuery.Open;
    Result := SelectQuery.RecordCount > 0;
    if Result then
      (SelectQuery.Fields[0] as TBLOBField).SaveToStream(ADst);
  finally
    SelectQuery.Close;
  end;
end;

If i comment "SelectQuery.Open;" then cursor doesn't switch. I supposed there should be option in TFDQuery (or connection), but i can't find anything. Any help?

UPDATE. As TLama suggested, i placed WaitCursor:TFDGUIxWaitCursor at my form (one place for app) and use it this way:

  StoredCursor := WaitCursor.ScreenCursor;
  WaitCursor.ScreenCursor := gcrNone;
  try
    // access DB with queries
  finally
    WaitCursor.ScreenCursor := StoredCursor;
  end;

UPDATE2: Two more ways to do it.

  1. Set TFDQuery.ResourceOptions.SilentMode=True (simplest way to disable hourglass cursor for specific queries, property name is bad, but according to doc it doesn't block any dialogs, only changes of cursor).
  2. Set global variable FADGUIxSilentMode=True from unit FireDAC.UI.Intf (not best, but probably simplest way to disable changes of cursor globally for FireDAC).
like image 483
Andrei Galatyn Avatar asked Sep 08 '14 12:09

Andrei Galatyn


1 Answers

This is covered in this FAQ set:

How can I turn off the SQL hourglass completely ?

a) To disable the wait cursor completely for an application, use TFDGUIxWaitCursor with Provider = 'Console'. The 'Console' provider contains an empty wait cursor implementation and the wait cursor will no longer be shown by FireDAC. If a mouse cursor is still changing, then check that only FireDAC.ConsoleUI.Wait unit is included into your application and FireDAC.VCLUI.Wait and FireDAC.FMXUI.Wait are not included. Note that you will not have the ability to turn the wait cursor on again.

b) To disable the wait cursor, but to have the ability to enable it again later, use code like the following:

FDWaitCursor1.ScreenCursor := gcrNone;

or

FDManager.ResourceOptions.SilentMode := True;

c) To disable the wait cursor and FireDAC dialogs, but to have the ability to enable them again later, set the FDManager.SilentMode property to True. This will disable all wait cursors and FireDAC dialogs, including:

  • The error dialog
  • Async execution dialog
  • Login dialog
  • Script progress dialog

Setting ResourceOptions.SilentMode to True disables only wait cursors.

Since you want to turn off that cursor only for that query execution, use the option b and wrap your code with something like this:

var
  OldCursor: TFDGUIxScreenCursor;
begin
  OldCursor := FDWaitCursor1.ScreenCursor;
  FDWaitCursor1.ScreenCursor := gcrNone;
  try
    // your code goes here
  finally
    FDWaitCursor1.ScreenCursor := OldCursor;
  end;
end;
like image 90
TLama Avatar answered Nov 05 '22 03:11

TLama