在apartment线程模型下,如何实现客户端的,多线程更新,以及分批取包
也就是客户程序的线程中如何实现动态生成的clientdataset的applyupdate &
getnextpacket方法的调用~代码如下:(大家还有什么好方法没有?)
tmythread=class(tthread)
private
mycon:TDCOMConnection;
myclientdataset:TClientDataSet;
source:tdatasource;
protected
procedure Execute;override;
public
shouldupdate,shoultgetnextpacket:boolean;
constructor create(sus:boolean;s:tdatasource);
destructor Destroy; override;
end;
var
Form1: TForm1;
myt1,myt2:tmythread ;implementation{$R *.dfm}{ tmythread }constructor tmythread.create(sus: boolean; s: tdatasource);
begin
inherited create(false);
mycon:=TDCOMConnection.Create(nil);
myclientdataset :=TClientDataSet.Create(nil);
self.FreeOnTerminate :=false;
mycon.ServerName :='Project2.xxx';
myclientdataset.RemoteServer :=self.mycon ;
myclientdataset.ProviderName :='datasetprovider1';
myclientdataset.PacketRecords :=3;
myclientdataset.FetchOnDemand :=false;
shouldupdate :=false;
shoultgetnextpacket :=false;
source:=s;
end;destructor tmythread.Destroy;
begin
if myclientdataset.Active then myclientdataset.Active :=false;
freeandnil(myclientdataset);
freeandnil(mycon);
inherited;
end;procedure tmythread.Execute;
begin
inherited;
CoInitialize(nil);
mycon.Connected :=true;
myclientdataset.Active :=true;
source.DataSet :=myclientdataset ;
while not Terminated do
begin
if shouldupdate then
begin
myclientdataset.ApplyUpdates(0);
shouldupdate:=false;
end;
if shoultgetnextpacket then
begin
myclientdataset.GetNextPacket;
shoultgetnextpacket:=false;
end;
end;
CoUninitialize;end;procedure TForm1.Button1Click(Sender: TObject);
begin
myt1:=tmythread.create(false,datasource1);end;procedure TForm1.Button2Click(Sender: TObject);
begin
myt2:=tmythread.create(false,datasource2);
end;procedure TForm1.Button3Click(Sender: TObject);
begin myt1.shouldupdate :=true;end;procedure TForm1.Button4Click(Sender: TObject);
begin
myt2.shoultgetnextpacket :=true;
end;
也就是客户程序的线程中如何实现动态生成的clientdataset的applyupdate &
getnextpacket方法的调用~代码如下:(大家还有什么好方法没有?)
tmythread=class(tthread)
private
mycon:TDCOMConnection;
myclientdataset:TClientDataSet;
source:tdatasource;
protected
procedure Execute;override;
public
shouldupdate,shoultgetnextpacket:boolean;
constructor create(sus:boolean;s:tdatasource);
destructor Destroy; override;
end;
var
Form1: TForm1;
myt1,myt2:tmythread ;implementation{$R *.dfm}{ tmythread }constructor tmythread.create(sus: boolean; s: tdatasource);
begin
inherited create(false);
mycon:=TDCOMConnection.Create(nil);
myclientdataset :=TClientDataSet.Create(nil);
self.FreeOnTerminate :=false;
mycon.ServerName :='Project2.xxx';
myclientdataset.RemoteServer :=self.mycon ;
myclientdataset.ProviderName :='datasetprovider1';
myclientdataset.PacketRecords :=3;
myclientdataset.FetchOnDemand :=false;
shouldupdate :=false;
shoultgetnextpacket :=false;
source:=s;
end;destructor tmythread.Destroy;
begin
if myclientdataset.Active then myclientdataset.Active :=false;
freeandnil(myclientdataset);
freeandnil(mycon);
inherited;
end;procedure tmythread.Execute;
begin
inherited;
CoInitialize(nil);
mycon.Connected :=true;
myclientdataset.Active :=true;
source.DataSet :=myclientdataset ;
while not Terminated do
begin
if shouldupdate then
begin
myclientdataset.ApplyUpdates(0);
shouldupdate:=false;
end;
if shoultgetnextpacket then
begin
myclientdataset.GetNextPacket;
shoultgetnextpacket:=false;
end;
end;
CoUninitialize;end;procedure TForm1.Button1Click(Sender: TObject);
begin
myt1:=tmythread.create(false,datasource1);end;procedure TForm1.Button2Click(Sender: TObject);
begin
myt2:=tmythread.create(false,datasource2);
end;procedure TForm1.Button3Click(Sender: TObject);
begin myt1.shouldupdate :=true;end;procedure TForm1.Button4Click(Sender: TObject);
begin
myt2.shoultgetnextpacket :=true;
end;
解决方案 »
- activex里访问webservices问题
- 李维《Inside VCL》中说到的拥有GUID的接口和COM接口
- 写入注册表时错误'Unable to write to \Software\xxx\xxx'
- dataset中用sql语句as出来的列,如何赋值?
- 100分!![Fatal Error] Unit1.pas(26): Could not create output file 'Unit1.dcu
- 调用DLL文件时,出现的问题!
- 求教tstringgrid控件的使用
- 哪有delphi 6.0中文版,注意不是汉化版
- 我写一个dll如何知道这个dll的路径?
- 我把能给的最高分给了!!!mssql数据库啊,我怎么连不上????!!!!
- 没有解决的问题,请大家帮忙
- 问一个很容易的算法问题,有四个数a,b,c,d..........
很不方便,不知道猛禽兄,对applyupdate之类的方法是如何调用的,线程里创建的midas
组件,如何在主线程中访问?如何设置调用时的context~
因为各个线程Apartment是各自独立的,当然应该在相应线程的Execute里执行,因为这里才是真正的线程内。
跨线程访问不是不行,对于COM应用来说,跨线程必须进行Marshall,麻烦不是一点两点了。
只知道进程,看来真是应该好好学习,不知道的东西太多了感谢猛禽兄~