FireDac freezes GUI

I work with FireDac under Delphi 10.1 Berlin.

To display data for the user, I use data controls such as TDBEdit.

I use TFDQuery and TDataSource to associate them with controls.

This works, but long sql queries that take some time for exectute will close the GUI.

I am wondering how to stop gui from freezing when doing these long requests.

I was thinking about background threads.

In the wiki, I read that FireDac can work with multithreads: http://docwiki.embarcadero.com/RADStudio/XE6/en/Multithreading_(FireDAC)

However, in the embarcadero community forums , the Jeff Overcash thread writes:

One thing that I have not seen, or the mention of Dmitry, is that you cannot TDataSource or LiveBindings against your requests with a background thread. If you run a background thread with a query that displays the results, you should disable LB or DataSource, open and extract all the data, then establish a connection.

The two will try to move the cursor over you or request a buffer for display, while the buffer is very volatile around in another topic.

I am wondering if someone who also uses FireDac and displays values ​​in a form can help me here.

+4
source share
1 answer

MSSql FireDAC. . , TQueryThread Execute , , , , , Execute while, Synchronize, / , , .

type

  TForm1 = class;

  TQueryThread = class(TThread)
  private
    FConnection: TFDConnection;
    FQuery: TFDQuery;
    FForm: TForm1;
  published
    constructor Create(AForm : TForm1);
    destructor Destroy; override;
    procedure Execute; override;
    procedure TransferData;
    property Query : TFDQuery read FQuery;
    property Connection : TFDConnection read FConnection;
    property Form : TForm1 read FForm;
  end;

  TForm1 = class(TForm)
    FDConnection1: TFDConnection;
    FDQuery1: TFDQuery;
    DataSource1: TDataSource;
    DBGrid1: TDBGrid;
    DBNavigator1: TDBNavigator;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  public
    QueryThread : TQueryThread;
  end;

[...]

constructor TQueryThread.Create(AForm : TForm1);
begin
  inherited Create(True);
  FreeOnTerminate := True;
  FForm := AForm;
  FConnection := TFDConnection.Create(Nil);
  FConnection.Params.Assign(Form.FDConnection1.Params);
  FConnection.LoginPrompt := False;

  FQuery := TFDQuery.Create(Nil);
  FQuery.Connection := Connection;
  FQuery.SQL.Text := Form.FDQuery1.SQL.Text;
end;

destructor TQueryThread.Destroy;
begin
  FQuery.Free;
  FConnection.Free;
  inherited;
end;

procedure TQueryThread.Execute;
begin
  Query.Open;
  Synchronize(TransferData);
end;

procedure TQueryThread.TransferData;
begin
  Form.FDQuery1.DisableControls;
  Form.FDQuery1.Data := Query.Data;
  Form.FDQuery1.EnableControls;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  QueryThread.Resume;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  QueryThread := TQueryThread.Create(Self);
end;

MJN , gui.

Btw, TClientDataSets, , FireDAC. , , , , " ", , FDConnection Params FDQuery Sql.

+5

All Articles