Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with Threads

Status
Not open for further replies.

lloydie2

Programmer
Apr 21, 2003
75
GB
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
===================================================
 
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?
 
If it might help, here's a small little demo program I did back when I was playing around with TThread before I found BeginThread. (code not related to threading cut out).

I don't think you have to forcibly terminate a thread within TThread if you use Create(False).

Code:
{ portions of a program written by Glenn9999 at tektips.com.  Purpose to demonstrate various tthread functions - puts an increasing count into a text label. }

type
  cthread = class(TThread)
  protected
  procedure doupdate;
  procedure execute;override;
  end;
var
  thread1: cthread;
  tno: integer;
  tbarmessage: string;
  minimized: boolean;

implementation

{$R *.DFM}

procedure cthread.doupdate;
  begin
    Application.Title := tbarmessage + IntToStr(tno);
    form1.label1.caption := IntToStr(tno);
  end;

procedure cthread.execute;
  var
    currentcount: integer;
  begin
    tbarmessage := 'Counting...';
    currentcount := 0;
    FreeOnTerminate := true;
    repeat
      inc(currentcount, 2);
      tno := currentcount;
      synchronize(doupdate);
    until Terminated;
  end;

procedure TForm1.Button5Click(Sender: TObject);
{ starts the thread process }
begin
  tno := 1;
  thread1 := cthread.create(false);
  thread1.priority := tpidle;
end;

end.
 
Thanks Glenn9999. I think I have the create(false) covered. I have managed to track down the offending thread. If I open my app and send some data to it, all seems to be fine. The problem I have is if I open the App and then close it without sending data.
For some reason the very first thread which is created cannot or is not destroyed on close. I am not sure what this thread is (probably the main thread for the app). Any Ideas?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top