Thread is executed properly, in the course of work, the only problem arises after closing the programs, namely there is no fault, only the program closes 4-5 seconds longer, freeze it, and the program closes neatly as it is not used thread.
The problem arose when I made the connection and transaction component of the thread, previously I have inherited from the main form and tied Query to them and used it, the program is regularly closed.
unit ThreadComboItemAdd;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, sqldb,mysql55conn,Forms,StdCtrls,Controls;
type
{TGetComboItems}
TGetComboItems = class(TThread)
private
S: String;
tCombo: TComboBox;
StrList: TStringList;
tField: String;
tFieldID: String;
tSQL: String;
tIndex: Integer;
tQ : TSQLQuery;
tConn : TMySQL55Connection;
tTrans : TSQLTransaction;
procedure SetEnable;
procedure SetDisable;
procedure PropertyQuery;
procedure AddToCombo;
procedure ShowStatus;
procedure SetCursorWait;
procedure SetCursorDefault;
public
IsFinish : Boolean;
constructor Create(sSQL,sField,sFieldID: String;
ComboBox: TComboBox; sIndex: Integer);
destructor Destroy; override;
protected
procedure Execute; override;
end;
implementation
uses glava1s;
{ TGetComboItem }
procedure TGetComboItems.SetEnable;
begin
Form1.ComboEnable:=true;
Form1.ComboObj:= tCombo;
Synchronize(@Form1.SetComboEnable);
end;
procedure TGetComboItems.SetDisable;
begin
Form1.ComboEnable:=false;
Form1.ComboObj := tCombo;
Synchronize(@Form1.SetComboEnable);
end;
procedure TGetComboItems.PropertyQuery;
begin
// kreiranje konekcije
tConn := TMySQL55Connection.Create(nil);
tConn.Hostname := 'localhost';
tConn.DatabaseName := 'baza';
tConn.UserName := 'root';
tConn.Password := 'root';
tConn.CharSet:='latin1';
tConn.Port:=3306;
// kreiranje transakcije
tTrans := TSQLTransaction.Create(nil);
tTrans.Database := tConn;
// kreiranje Query-a
tQ := TSQLQuery.Create(nil);
tQ.DataBase := tConn;
tQ.Transaction := tTrans;
tConn.Transaction := tTrans;
// Otvaranje konekcije i tabele
tConn.Connected:=true;
tQ.SQL.Add(tSQL);
end;
constructor TGetComboItems.Create(sSQL, sField, sFieldID: String;
ComboBox: TComboBox; sIndex: Integer);
begin
// kreiranje thread-a
FreeOnTerminate:=true;
StrList := TStringList.Create;
tField := sField;
tFieldID:= sFieldID;
tIndex := sIndex;
tCombo := ComboBox;
tSQL := sSQL;
IsFinish:= false;
inherited Create(true);
end;
destructor TGetComboItems.Destroy;
begin
// Oslobađanje komponenti
FreeAndNil(StrList);
FreeAndNil(tConn);
FreeAndNil(tQ);
FreeAndNil(tTrans);
inherited Destroy;
end;
procedure TGetComboItems.Execute;
begin
// izvršavanje thread-a
//if not Terminated then
begin
Synchronize(@SetDisable);
Synchronize(@SetCursorWait);
s:='Učitavanje podataka je u toku...';
Synchronize(@ShowStatus);
PropertyQuery;
with tQ do
Begin
Open;
while not eof do
begin
if (not IsFinish) then
begin
StrList.AddObject(FieldByName(tField).AsString
,TObject(FieldByName(tFieldID).AsInteger));
Synchronize(@ShowStatus);
Next;
end
else
Last;
end;
close;
tConn.Transaction.Commit;
tConn.Connected:=false;
end;
IsFinish:=true;
s:='Učitavanje podataka je izvršeno.';
Synchronize(@ShowStatus);
Synchronize(@AddToCombo);
Synchronize(@SetCursorDefault);
Synchronize(@SetEnable);
end;
end;
procedure TGetComboItems.AddToCombo;
begin
// Punjenje ComboBox-a na traženoj formi
Form1.ComboObj:=tCombo;
Form1.StrList:=StrList;
Form1.ComboIndex:=tIndex;
Synchronize(@Form1.SetComboItems);
end;
procedure TGetComboItems.ShowStatus;
begin
// Ispis statusa thread-a
Form1.StatusText:=s;
Synchronize(@Form1.SetStatusText);
end;
procedure TGetComboItems.SetCursorWait;
begin
Screen.Cursor:=crSQLWait;
end;
procedure TGetComboItems.SetCursorDefault;
begin
Screen.Cursor:=crDefault;
end;
end.
What is the problem?