I am having my first foray into using threads, but I am coming unstuck when trying to use sql in one of the threads. I suspect That I am not creating an instance of the database connection.
Also I think I am also have a problem destroying the threads when I close the App. Can you Help?
Main Thread
===========================================
procedure TMyDataParse1.execute;
var
p: String;
EndStr, StrCheck: integer;
begin
Regex1 := TPerlRegEx.Create(nil);
Regex1.RegEx := RegExpStr1;
Regex1.Subject := FDataString;
if Regex1.Match then begin
StrCheck := 1;
if RegExpEnd1 = '1' then begin
EndStr := 1;
StrCheck := 0;
end
else
Endstr := 0;
StrCheck :=1;
end;
if RegExpEnd1 <> '1' then begin
Regex1.RegEx := RegExpEnd1;
Regex1.Subject := FDataString;;
if Regex1.Match then begin
EndStr := 1;
StrCheck := 0;
end
else begin
EndStr := 0;
StrCheck := 1;
end;
end;
if EndStr = 1 then begin
sysStr1 := sysStr1+FDataString;
CallString := CallConv(sysStr1,PhonesysID1);
CallString := CallString+',1,1';
Synchronize(SendCSVData);
EndStr := 0;
SysStr1 := '';
StrCheck := 0;
end
else begin
if StrCheck = 1 then
SysStr1 := SysStr1+FDataString+#13#10;
end;
end;
procedure TMyDataParse1.SendCSVData;
begin
TMyDataCalcuLate1.Create(CallString); // send data to new thread
Form1.SQLOutData(CallString);
end;
===================================================
TMyDataCalcuLate1.pas thread
===================================================
uses
Windows, Messages, SysUtils, Classes, Forms, Dialogs, DateUtils,StdCtrls,DB,passqlite, passql, ComCtrls;
Type
TMyDataCalcuLate1=class(TThread)
private
ThreadCollexDB : TliteDB;
FCalString: String;
SiteID,SysTimeBandStart, SysTimeBandEnd, SysOffset, SysTOffset :String;
CallInsert : Pchar;
public
constructor Create(CalString:String);
procedure execute; override;
function split(Text : string; Token : char ; WordNo : integer):string;
Procedure SendSql;
end;
implementation
uses
Unit1;
constructor TMyDataCalcuLate1.Create(CalString: String);
begin
FreeOnTerminate :=True;
FCalString := CalString;
inherited Create(False);
end;
Procedure TMyDataCalcuLate1.Execute;
var
debug : integer;
begin
Try
debug := 0;
With ThreadCollexDB do
Begin
ThreadCollexDB := TliteDB.Create(nil, ExtractFilePath(Application.ExeName)+' end;
SIteID := '1';
{**************************************************
Get System setting from Database
***************************************************}
ThreadCollexDB.Query('SELECT * from sys_col where col_active = 1 and col_id ='''+SiteID+'''');
if ThreadCollexDB.RowCount > 0 then begin
SysTimeBandStart := ThreadCollexDB.Results[0][7];
SysTimeBandEnd := ThreadCollexDB.Results[0][8];
end;
end;
end
===================================================
Also I think I am also have a problem destroying the threads when I close the App. Can you Help?
Main Thread
===========================================
procedure TMyDataParse1.execute;
var
p: String;
EndStr, StrCheck: integer;
begin
Regex1 := TPerlRegEx.Create(nil);
Regex1.RegEx := RegExpStr1;
Regex1.Subject := FDataString;
if Regex1.Match then begin
StrCheck := 1;
if RegExpEnd1 = '1' then begin
EndStr := 1;
StrCheck := 0;
end
else
Endstr := 0;
StrCheck :=1;
end;
if RegExpEnd1 <> '1' then begin
Regex1.RegEx := RegExpEnd1;
Regex1.Subject := FDataString;;
if Regex1.Match then begin
EndStr := 1;
StrCheck := 0;
end
else begin
EndStr := 0;
StrCheck := 1;
end;
end;
if EndStr = 1 then begin
sysStr1 := sysStr1+FDataString;
CallString := CallConv(sysStr1,PhonesysID1);
CallString := CallString+',1,1';
Synchronize(SendCSVData);
EndStr := 0;
SysStr1 := '';
StrCheck := 0;
end
else begin
if StrCheck = 1 then
SysStr1 := SysStr1+FDataString+#13#10;
end;
end;
procedure TMyDataParse1.SendCSVData;
begin
TMyDataCalcuLate1.Create(CallString); // send data to new thread
Form1.SQLOutData(CallString);
end;
===================================================
TMyDataCalcuLate1.pas thread
===================================================
uses
Windows, Messages, SysUtils, Classes, Forms, Dialogs, DateUtils,StdCtrls,DB,passqlite, passql, ComCtrls;
Type
TMyDataCalcuLate1=class(TThread)
private
ThreadCollexDB : TliteDB;
FCalString: String;
SiteID,SysTimeBandStart, SysTimeBandEnd, SysOffset, SysTOffset :String;
CallInsert : Pchar;
public
constructor Create(CalString:String);
procedure execute; override;
function split(Text : string; Token : char ; WordNo : integer):string;
Procedure SendSql;
end;
implementation
uses
Unit1;
constructor TMyDataCalcuLate1.Create(CalString: String);
begin
FreeOnTerminate :=True;
FCalString := CalString;
inherited Create(False);
end;
Procedure TMyDataCalcuLate1.Execute;
var
debug : integer;
begin
Try
debug := 0;
With ThreadCollexDB do
Begin
ThreadCollexDB := TliteDB.Create(nil, ExtractFilePath(Application.ExeName)+' end;
SIteID := '1';
{**************************************************
Get System setting from Database
***************************************************}
ThreadCollexDB.Query('SELECT * from sys_col where col_active = 1 and col_id ='''+SiteID+'''');
if ThreadCollexDB.RowCount > 0 then begin
SysTimeBandStart := ThreadCollexDB.Results[0][7];
SysTimeBandEnd := ThreadCollexDB.Results[0][8];
end;
end;
end
===================================================