unit MainUnit;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls,ArrayDMLThreadUnit,
  FireDAC.Stan.Intf, FireDAC.Stan.Option, FireDAC.Stan.Param,
  FireDAC.Stan.Error, FireDAC.DatS, FireDAC.Phys.Intf, FireDAC.DApt.Intf,
  FireDAC.Stan.Async, FireDAC.DApt, Data.DB, FireDAC.Comp.DataSet,
  FireDAC.Comp.Client, Vcl.ExtCtrls, Vcl.ComCtrls, Vcl.Grids, Vcl.DBGrids,
  NamesThread, FireDAC.UI.Intf, FireDAC.Stan.Def, FireDAC.Stan.Pool,
  FireDAC.Phys, FireDAC.Phys.MySQL, FireDAC.Phys.MySQLDef, FireDAC.VCLUI.Wait,FamiliesThread;

type
  TForm1 = class(TForm)
    TopPanel: TPanel;
    AddRecords: TButton;
    StatusBar1: TStatusBar;
    NamePanel: TPanel;
    bOpenNames: TButton;
    DBGridNames: TDBGrid;
    FamilyPanel: TPanel;
    bOpenFamilies: TButton;
    DBGridFamilies: TDBGrid;
    bTerminate: TButton;
    procedure AddRecordsClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure bOpenNamesClick(Sender: TObject);
    procedure bOpenFamiliesClick(Sender: TObject);
    procedure bTerminateClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  ArrayDMLThread:TArrayDMLThread;
  NamesThread:TNamesThread;
  FamiliesThread:TFamiliesThread;

implementation

{$R *.dfm}







procedure TForm1.AddRecordsClick(Sender: TObject);
var s:string;
begin

try
StatusBar1.Panels[0].Text:='';
InputQuery('Сколько записей добавить?','Число записей',s);
ArrayDMLThreadUnit.ArraySize:=s.ToInteger();
except

end;

// if not Assigned(ArrayDMLThread) then

 if Assigned(ArrayDMLThread) and not (ArrayDMLThread.CheckTerminated) then

 begin
   ArrayDMLThread.Terminate;
   ArrayDMLThread.Free;


 end;


  ArrayDMLThread:=TArrayDMLThread.Create(false);
  ArrayDMLThread.FreeOnTerminate:=false;

end;

procedure TForm1.bOpenFamiliesClick(Sender: TObject);
begin
if Assigned(FamiliesThread) and not ( FamiliesThread.CheckTerminated) then
   begin
      FamiliesThread.Terminate;
      FamiliesThread.Free;
   end;
//Запускаем новый экземпляр
FamiliesThread:=TFamiliesThread.Create(false);
FamiliesThread.FreeOnTerminate:=false;
end;

procedure TForm1.bOpenNamesClick(Sender: TObject);
begin


//Освобождаем память от предыдущего экземпляра потока
if Assigned(NamesThread) and not (NamesThread.CheckTerminated) then
   begin
      NamesThread.Terminate;
      NamesThread.Free;
   end;

//Запускаем новый экземпляр
NamesThread:=TNamesThread.Create(false);
NamesThread.FreeOnTerminate:=false;


end;

procedure TForm1.bTerminateClick(Sender: TObject);
begin
NamesThread.Terminate;
//if NamesThread.Finished then showmessage('Terminated');
NamesThread.Free;

end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin

//if Assigned(ArayDMLThread) then ArayDMLThread.Terminate;
Application.Terminate;
end;

end.
