Z
zhyhui2008
Unregistered / Unconfirmed
GUEST, unregistred user!
我用线程查询数据库里的数据,怎么才能把查到的记录数传递给主窗体的一个公共变量,
下面是我的线程代码:
unit U_Qry_Thread;
interface
uses
SysUtils, Classes, DB, ADODB, DBClient, MConnect, SConnect,Windows,Dialogs, Messages,inifiles;
type
Qry_Thread = class(TThread)
private
{ Private declarations }
myinifile:TIniFile;
socket:TSocketConnection;
client:TClientDataSet;
fdatasource:TDataSource;
fthread_str:String;
f_count:Integer;
//变量
procedure Qry_Excete;
procedure showResult;
protected
procedure Execute;
override;{ 执行线程的方法 }
public
constructor Create(p_str,dsp_name:String;DataSource:TDataSource);
virtual;
{ 线程构造器 }
end;
implementation
uses U_Main, U_public, U_lytj, U_data;
{Qry_Thread}
procedure Qry_Thread.showResult;
begin
fdatasource.DataSet:=client;
// frm_lytj.Edit1.Text:=IntToStr(f_count);
end;
procedure Qry_Thread.Qry_Excete;
begin
with clientdo
begin
Close;
CommandText:= fthread_str;
Open;
end;
f_count:=client.RecordCount;
end;
procedure Qry_Thread.Execute;
begin
try
Synchronize(Qry_Excete);
Synchronize(showResult);
Except
ShowMessage('Query Error');
{ 线程异常 }
end;
end;
constructor Qry_Thread.Create(p_str,dsp_name:String;DataSource:TDataSource);
{ Place thread code here }
var
aHost,aServiceAddr,filename:string;
aport:Integer;
begin
filename:=ExtractFilePath(paramstr(0))+'config.ini';
myinifile:=Tinifile.Create(filename);
aServiceAddr:=myiniFile.ReadString('SERVER', 'ServiceAdd', '');
aHost:= myiniFile.ReadString('SERVER', 'ServiceHost', '');
aport:=StrToInt(myinifile.ReadString('SERVER','Port',''));
socket:=TSocketConnection.Create(nil);
socket.Address:=aServiceAddr;
socket.Host:=aHost;
socket.Port:=aport;
socket.ServerGUID:='{AF4DFB6F-EE54-4520-AE7F-3F0839A9350B}';
socket.ServerName:='P_server.tydgl_server';
socket.Connected:=true;
client:=TClientDataSet.Create(nil);
client.RemoteServer:=socket;
client.ProviderName:=dsp_name;
fdatasource:=DataSource;
fthread_str:=p_str;
// f_count:=qry_count;
inherited Create(false);
FreeOnTerminate := true;
end;
下面是我的线程代码:
unit U_Qry_Thread;
interface
uses
SysUtils, Classes, DB, ADODB, DBClient, MConnect, SConnect,Windows,Dialogs, Messages,inifiles;
type
Qry_Thread = class(TThread)
private
{ Private declarations }
myinifile:TIniFile;
socket:TSocketConnection;
client:TClientDataSet;
fdatasource:TDataSource;
fthread_str:String;
f_count:Integer;
//变量
procedure Qry_Excete;
procedure showResult;
protected
procedure Execute;
override;{ 执行线程的方法 }
public
constructor Create(p_str,dsp_name:String;DataSource:TDataSource);
virtual;
{ 线程构造器 }
end;
implementation
uses U_Main, U_public, U_lytj, U_data;
{Qry_Thread}
procedure Qry_Thread.showResult;
begin
fdatasource.DataSet:=client;
// frm_lytj.Edit1.Text:=IntToStr(f_count);
end;
procedure Qry_Thread.Qry_Excete;
begin
with clientdo
begin
Close;
CommandText:= fthread_str;
Open;
end;
f_count:=client.RecordCount;
end;
procedure Qry_Thread.Execute;
begin
try
Synchronize(Qry_Excete);
Synchronize(showResult);
Except
ShowMessage('Query Error');
{ 线程异常 }
end;
end;
constructor Qry_Thread.Create(p_str,dsp_name:String;DataSource:TDataSource);
{ Place thread code here }
var
aHost,aServiceAddr,filename:string;
aport:Integer;
begin
filename:=ExtractFilePath(paramstr(0))+'config.ini';
myinifile:=Tinifile.Create(filename);
aServiceAddr:=myiniFile.ReadString('SERVER', 'ServiceAdd', '');
aHost:= myiniFile.ReadString('SERVER', 'ServiceHost', '');
aport:=StrToInt(myinifile.ReadString('SERVER','Port',''));
socket:=TSocketConnection.Create(nil);
socket.Address:=aServiceAddr;
socket.Host:=aHost;
socket.Port:=aport;
socket.ServerGUID:='{AF4DFB6F-EE54-4520-AE7F-3F0839A9350B}';
socket.ServerName:='P_server.tydgl_server';
socket.Connected:=true;
client:=TClientDataSet.Create(nil);
client.RemoteServer:=socket;
client.ProviderName:=dsp_name;
fdatasource:=DataSource;
fthread_str:=p_str;
// f_count:=qry_count;
inherited Create(false);
FreeOnTerminate := true;
end;