D
del_man
Unregistered / Unconfirmed
GUEST, unregistred user!
各位高人,下面是小弟做的一个多线程的测试,我想把它放在中间层服务器上跑。小第对多线程从来没有接触过,看了几个例子盗版了一个,不知道对错,请大家不要见笑。
unit mrp130ya;
interface
uses
Windows, Messages, SysUtils, Classes, ComServ, ComObj, VCLCom, DataBkr,Dialogs,
DBClient, ymrp130_TLB, StdVcl, Db, ADODB,Controls, Provider,ActiveX;
type
Tmythread = class(TThread)
private
{ Private declarations }
FConnection:TAdoConnection;
FQuery:TAdoQuery;
FProvider:TDatasetprovider;
procedure ConnectDataSource;
protected
procedure Execute;
override;
public
Constructor Create(Suspended:Boolean;Query:TAdoquery;Provider:TDatasetprovider);
destructor Destroy;override;
end;
implementation
{ Important: Methods and properties of objects in VCL can only be used in a
method called using Synchronize, for example,
Synchronize(UpdateCaption);
and UpdateCaption could look like,
procedure Tmythread.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end;
}
{ Tmythread }
procedure Tmythread.ConnectDataSource;
begin
FProvider.DataSet:=FQuery;
end;
constructor Tmythread.Create(Suspended: Boolean;
Query: TAdoquery;
Provider: TDatasetprovider);
begin
CoInitialize(nil);
try
FConnection:=TAdoConnection.Create(nil);
FQuery:=query;
FProvider:=Provider;
FQuery.Connection:=FConnection;
with FConnectiondo
begin
LoginPrompt:=False;
KeepConnection:=true;
ConnectionString:='Provider=MSDASQL.1;Password=123;Persist Security Info=True;User ID=123;Data Source=123';
end;
inherited Create(Suspended);
except
on e:Exceptiondo
begin
showmessage(e.Message);
end;
end;
end;
destructor Tmythread.Destroy;
begin
FConnection.free;
CoUninitialize;
inherited Destroy;
end;
procedure Tmythread.Execute;
begin
try
FreeOnTerminate:=true;
Fquery.Close;
Fquery.SQL.Clear;
Fquery.SQL.Add('select * from t1');
Fquery.Open;
Synchronize(ConnectDataSource);
except
end;
end;
end.
然后我在中间层定义好了一个方法
procedure Tdmmrp130y.test(var data: OleVariant);
begin
Tmythread.Create(false,ADOQuery2,DataSetProvider1);
//do other ...
data:=DataSetProvider1.data;
end;
现在的问题是1。DataSetProvider1.data 是不是就是线程查询出来的数据集?
2。data:=DataSetProvider1.data;这句话必须要等线程结束了才可以执行,
我该怎么等待?
小弟只有这些分了,如果不过,等我以后有分了另外补上,谢谢!!!
unit mrp130ya;
interface
uses
Windows, Messages, SysUtils, Classes, ComServ, ComObj, VCLCom, DataBkr,Dialogs,
DBClient, ymrp130_TLB, StdVcl, Db, ADODB,Controls, Provider,ActiveX;
type
Tmythread = class(TThread)
private
{ Private declarations }
FConnection:TAdoConnection;
FQuery:TAdoQuery;
FProvider:TDatasetprovider;
procedure ConnectDataSource;
protected
procedure Execute;
override;
public
Constructor Create(Suspended:Boolean;Query:TAdoquery;Provider:TDatasetprovider);
destructor Destroy;override;
end;
implementation
{ Important: Methods and properties of objects in VCL can only be used in a
method called using Synchronize, for example,
Synchronize(UpdateCaption);
and UpdateCaption could look like,
procedure Tmythread.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end;
}
{ Tmythread }
procedure Tmythread.ConnectDataSource;
begin
FProvider.DataSet:=FQuery;
end;
constructor Tmythread.Create(Suspended: Boolean;
Query: TAdoquery;
Provider: TDatasetprovider);
begin
CoInitialize(nil);
try
FConnection:=TAdoConnection.Create(nil);
FQuery:=query;
FProvider:=Provider;
FQuery.Connection:=FConnection;
with FConnectiondo
begin
LoginPrompt:=False;
KeepConnection:=true;
ConnectionString:='Provider=MSDASQL.1;Password=123;Persist Security Info=True;User ID=123;Data Source=123';
end;
inherited Create(Suspended);
except
on e:Exceptiondo
begin
showmessage(e.Message);
end;
end;
end;
destructor Tmythread.Destroy;
begin
FConnection.free;
CoUninitialize;
inherited Destroy;
end;
procedure Tmythread.Execute;
begin
try
FreeOnTerminate:=true;
Fquery.Close;
Fquery.SQL.Clear;
Fquery.SQL.Add('select * from t1');
Fquery.Open;
Synchronize(ConnectDataSource);
except
end;
end;
end.
然后我在中间层定义好了一个方法
procedure Tdmmrp130y.test(var data: OleVariant);
begin
Tmythread.Create(false,ADOQuery2,DataSetProvider1);
//do other ...
data:=DataSetProvider1.data;
end;
现在的问题是1。DataSetProvider1.data 是不是就是线程查询出来的数据集?
2。data:=DataSetProvider1.data;这句话必须要等线程结束了才可以执行,
我该怎么等待?
小弟只有这些分了,如果不过,等我以后有分了另外补上,谢谢!!!