lloydie-t
asked on
Help with Threads
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,PhonesysI D1);
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(C allString) ; // send data to new thread
Form1.SQLOutData(CallStrin g);
end;
========================== ========== ========== =====
TMyDataCalcuLate1.pas thread
========================== ========== ========== =====
uses
Windows, Messages, SysUtils, Classes, Forms, Dialogs, DateUtils,StdCtrls,DB,pass qlite, passql, ComCtrls;
Type
TMyDataCalcuLate1=class(TT hread)
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(C alString: 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(Applicatio n.ExeName) +'www\coll ex.sdb');
end;
SIteID := '1';
{************************* ********** ********** *****
Get System setting from Database
************************** ********** ********** *****}
ThreadCollexDB.Query('SELE CT * 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,PhonesysI
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(C
Form1.SQLOutData(CallStrin
end;
==========================
TMyDataCalcuLate1.pas thread
==========================
uses
Windows, Messages, SysUtils, Classes, Forms, Dialogs, DateUtils,StdCtrls,DB,pass
Type
TMyDataCalcuLate1=class(TT
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(C
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(Applicatio
end;
SIteID := '1';
{*************************
Get System setting from Database
**************************
ThreadCollexDB.Query('SELE
if ThreadCollexDB.RowCount > 0 then begin
SysTimeBandStart := ThreadCollexDB.Results[0][
SysTimeBandEnd := ThreadCollexDB.Results[0][
end;
end;
end
==========================
ASKER
No COM here using passqlite to access sqlite database
But what about TPerlRegEx? And are you sure that there are no COM interfaces used in PasSQLLite?
ASKER
I am fairly Positive. PasSqlite.pas is a delphi wrapper which uses sqlite3.dll.
On a normal form I would do.
public
{ Public declarations }
LiteDB1 : Tlitedb;
blah
.........
.....
begin
With LiteDB1 do
Begin
LiteDB1 := TliteDB.Create(self, ExtractFilePath(Applicatio n.ExeName) +'www\coll ex.sdb3');
end;
do some sqlite stuff
.............
............
LiteDB1.close
end.
On a normal form I would do.
public
{ Public declarations }
LiteDB1 : Tlitedb;
blah
.........
.....
begin
With LiteDB1 do
Begin
LiteDB1 := TliteDB.Create(self, ExtractFilePath(Applicatio
end;
do some sqlite stuff
.............
............
LiteDB1.close
end.
ASKER
Doh. Wrong Database name! I am still have a few problems when I close the app. I suspect I am opening a thread but not closing all the threads which are open. Do I need to force 'terminate'?
Do I need to create a destructor for each thread?
Do I need to create a destructor for each thread?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Forced accept.
Computer101
EE Admin
Computer101
EE Admin
(uses ActiveX)
Procedure TMyDataCalcuLate1.Execute;
begin
CoInitializeEx(nil, 0); // <-------------------Add this.
try
<Your code here>
finally CoUnInitialize; // <-------------------Add this.
end;
end;
Not sure if your database relies on COM in some places, though so I might be mistaken.