N
njhuadong
Unregistered / Unconfirmed
GUEST, unregistred user!
在一个TIMER中启动一个数据库查询线程,当时间间隔比较短时,数据查询不出来,而且一段时间后,机器会没有相应,代码如下:
unit UntThread;
interface
uses
Classes,adodb,activeX;
type
AnswerReq = class(TThread)
private
{ Private declarations }
FMsgContent : string;
FSrcAddr : string;
FDestAddr : string;
FRegState : string;
FIsRegSyc : integer;
FConnect : string;
FTemp : string;
FQuery : TAdoQuery;
FAdoCon : TAdoConnection;
protected
procedure Execute;
override;
procedure MonitorShow;
procedure MonitorTemp;
public
constructor Create(MsgContent,SrcAddr,DestAddr,RegState:string;IsRegSyc:integer;AConnect:string);
end;
implementation
uses UntMain,UntWatch,UntData, DB;
{ Important: Methods and properties of objects in VCL or CLX can only be used
in a method called using Synchronize, for example,
Synchronize(UpdateCaption);
and UpdateCaption could look like,
procedure AnswerReq.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end;
}
{ AnswerReq }
constructor AnswerReq.Create(MsgContent,SrcAddr,DestAddr,RegState:string;IsRegSyc:integer;AConnect:string);
begin
FMsgContent := MsgContent;
FSrcAddr := SrcAddr;
FDestAddr := DestAddr;
FRegState := RegState;
FIsRegSyc := IsRegSyc;
FConnect := AConnect;
CoInitialize(nil);
FAdoCon := TADOConnection.create(nil);
with FAdoCondo
begin
ConnectionString := FConnect;
LoginPrompt := false;
Connected := true;
end;
FQuery := TADOQuery.Create(nil);
FQuery.Connection := FAdoCon;
inherited Create(False);
FreeOnTerminate := true;
end;
procedure AnswerReq.Execute;
var sSql : string;
AdoCon : tADoconnection;
AdoQ : tAdoQuery;
begin
{ Place thread code here }
Synchronize(MonitorShow);
if copy(FMsgContent,1,2) = 'abcd' then
begin
with FQuerydo
begin
SQL.Clear;
SQL.Add('SELECT * FROM TBTABLE');
open;
FTemp := fieldbyname('AAA').AsString;
Synchronize(MonitorTemp);
close;
end;
FQuery.Free;
FAdoCon.Free;
end;
end;
procedure AnswerReq.MonitorShow;
var sType,sContent : string;
begin
with FrmWatchdo
begin
Showbegin
fo('开始线程!');
end;
end;
procedure AnswerReq.MonitorTemp;
begin
with FrmWatchdo
begin
ShowFailInfo(FTemp);
end;
end;
end.
TIMER 调用:
with AnswerReq.Create(StrPas(MsgContent),StrPas(SrcAddr),StrPas(DestAddr),strpas(RegState),IsRegSync,sConnect)do
OnTerminate := Threadend;
unit UntThread;
interface
uses
Classes,adodb,activeX;
type
AnswerReq = class(TThread)
private
{ Private declarations }
FMsgContent : string;
FSrcAddr : string;
FDestAddr : string;
FRegState : string;
FIsRegSyc : integer;
FConnect : string;
FTemp : string;
FQuery : TAdoQuery;
FAdoCon : TAdoConnection;
protected
procedure Execute;
override;
procedure MonitorShow;
procedure MonitorTemp;
public
constructor Create(MsgContent,SrcAddr,DestAddr,RegState:string;IsRegSyc:integer;AConnect:string);
end;
implementation
uses UntMain,UntWatch,UntData, DB;
{ Important: Methods and properties of objects in VCL or CLX can only be used
in a method called using Synchronize, for example,
Synchronize(UpdateCaption);
and UpdateCaption could look like,
procedure AnswerReq.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end;
}
{ AnswerReq }
constructor AnswerReq.Create(MsgContent,SrcAddr,DestAddr,RegState:string;IsRegSyc:integer;AConnect:string);
begin
FMsgContent := MsgContent;
FSrcAddr := SrcAddr;
FDestAddr := DestAddr;
FRegState := RegState;
FIsRegSyc := IsRegSyc;
FConnect := AConnect;
CoInitialize(nil);
FAdoCon := TADOConnection.create(nil);
with FAdoCondo
begin
ConnectionString := FConnect;
LoginPrompt := false;
Connected := true;
end;
FQuery := TADOQuery.Create(nil);
FQuery.Connection := FAdoCon;
inherited Create(False);
FreeOnTerminate := true;
end;
procedure AnswerReq.Execute;
var sSql : string;
AdoCon : tADoconnection;
AdoQ : tAdoQuery;
begin
{ Place thread code here }
Synchronize(MonitorShow);
if copy(FMsgContent,1,2) = 'abcd' then
begin
with FQuerydo
begin
SQL.Clear;
SQL.Add('SELECT * FROM TBTABLE');
open;
FTemp := fieldbyname('AAA').AsString;
Synchronize(MonitorTemp);
close;
end;
FQuery.Free;
FAdoCon.Free;
end;
end;
procedure AnswerReq.MonitorShow;
var sType,sContent : string;
begin
with FrmWatchdo
begin
Showbegin
fo('开始线程!');
end;
end;
procedure AnswerReq.MonitorTemp;
begin
with FrmWatchdo
begin
ShowFailInfo(FTemp);
end;
end;
end.
TIMER 调用:
with AnswerReq.Create(StrPas(MsgContent),StrPas(SrcAddr),StrPas(DestAddr),strpas(RegState),IsRegSync,sConnect)do
OnTerminate := Threadend;