Link to home
Start Free TrialLog in
Avatar of lloydie-t
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,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)+'www\collex.sdb');
  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
===================================================


Avatar of Wim ten Brink
Wim ten Brink
Flag of Netherlands image

You might need to initialize the COM functionality in your code, like this:

(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.
Avatar of lloydie-t
lloydie-t

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?
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(Application.ExeName)+'www\collex.sdb3');
                end;

do some sqlite stuff
.............
............

LiteDB1.close
end.

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?
ASKER CERTIFIED SOLUTION
Avatar of Wim ten Brink
Wim ten Brink
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Forced accept.

Computer101
EE Admin