unit NamesThread;

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,vcl.dialogs;

type
  TNamesThread = class(TThread)
  FDConnectionNames:TFDConnection;
  FDQueryNames:TFDQuery;
  DataSourceNames:TDataSource;

  private




    procedure showConnected;
    procedure showNamesOnMainForm;

  public



  function CheckTerminated:boolean;

  destructor Destroy;override;



  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 }
{
constructor TNamesThread.Create;
begin
inherited Create(Self);

showmessage('Поток запущен');

end;}



function TNamesThread.CheckTerminated: boolean;
begin
if Self.Terminated then Result:=true else
Result:=false;
end;

destructor TNamesThread.Destroy;
begin


 FDConnectionNames.Free;
 FDQueryNames.Free;
 DataSourceNames.Free;

  inherited;

end;

procedure TNamesThread.Execute;
begin
  NameThreadForDebugging('MyNamesThread');
  { Place thread code here }

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

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

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


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

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

if Self.Terminated then MainUnit.form1.StatusBar1.Panels[0].Text:=('Terminated') else
MainUnit.form1.StatusBar1.Panels[0].Text:=(' Not Terminated');




end;

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



procedure TNamesThread.showNamesOnMainForm;
begin
MainUnit.Form1.DBGridNames.DataSource:=DataSourceNames;
MainUnit.Form1.DBGridNames.Columns[0].FieldName:='idUsers';
MainUnit.Form1.DBGridNames.Columns[0].Width:=50;

MainUnit.Form1.DBGridNames.Columns[1].FieldName:='name';
MainUnit.Form1.DBGridNames.Columns[1].Width:=80;



end;

end.
