unit FamiliesThread;

interface

uses
System.SysUtils, System.Classes, FireDAC.Stan.Intf, FireDAC.Stan.Option,
  FireDAC.Stan.Error, FireDAC.UI.Intf, FireDAC.Phys.Intf, FireDAC.Stan.Def,
  FireDAC.Phys, FireDAC.Comp.Client, FireDAC.Phys.MySQLDef, FireDAC.Phys.MySQL,
  FireDAC.Stan.Param, FireDAC.DatS, FireDAC.DApt.Intf, FireDAC.Stan.Async,
  FireDAC.DApt, Data.DB, FireDAC.Comp.DataSet;

type
  TFamiliesThread = class(TThread)
  FDConnectionFamilies:TFDConnection;
  FDQueryFamilies:TFDQuery;
  DataSourceFamilies:TDataSource;



  procedure showConnected;
  procedure showFamiliesOnMainForm;

  protected
    procedure Execute; override;
  end;

implementation

{
  Important: Methods and properties of objects in visual components can only be
  used in a method called using Synchronize, for example,

      Synchronize(UpdateCaption);

  and UpdateCaption could look like,

    procedure TNamesThread.UpdateCaption;
    begin
      Form1.Caption := 'Updated in a thread';
    end;

    or

    Synchronize(
      procedure
      begin
        Form1.Caption := 'Updated in thread via an anonymous method'
      end
      )
    );

  where an anonymous method is passed.

  Similarly, the developer can call the Queue method with similar parameters as
  above, instead passing another TThread class as the first parameter, putting
  the calling thread in a queue with the other thread.

}

uses MainUnit;

{ TNamesThread }

procedure TFamiliesThread.Execute;
begin

  { Place thread code here }

//Создаем соединение
   FDConnectionFamilies:=TFDConnection.Create(nil);
   FDConnectionFamilies.ConnectionDefName:='MyConnDefName';
   FDConnectionFamilies.Connected:=true;
   if FDConnectionFamilies.Connected then Synchronize(showConnected);

//Создаем FDQuery
   FDQueryFamilies:=TFDQuery.Create(nil);
   FDQueryFamilies.Connection:=FDConnectionFamilies;

//Делаем запрос
   FDQueryFamilies.SQL.Clear;
   FDQueryFamilies.SQL.Text:='SELECT idUsers,Family FROM aphina_db.users;';
   FDQueryFamilies.Open;


//Создаем DataSource
DataSourceFamilies:=TDataSource.Create(nil);
DataSourceFamilies.DataSet:=FDQueryFamilies;
DataSourceFamilies.Enabled:=true;

//Отображаем на главной форме
Synchronize(showFamiliesOnMainForm);

end;

procedure TFamiliesThread.showConnected;
begin
MainUnit.Form1.StatusBar1.Panels[0].Text:='FDConnectionFamilies Connected';
end;



procedure TFamiliesThread.showFamiliesOnMainForm;
begin
MainUnit.Form1.DBGridFamilies.DataSource:=DataSourceFamilies;
MainUnit.Form1.DBGridFamilies.Columns[0].FieldName:='idUsers';
MainUnit.Form1.DBGridFamilies.Columns[0].Width:=50;

MainUnit.Form1.DBGridFamilies.Columns[1].FieldName:='Family';
MainUnit.Form1.DBGridFamilies.Columns[1].Width:=80;


end;

end.
