首页 技术 正文
技术 2022年11月15日
0 收藏 744 点赞 4,661 浏览 1354 个字

早在2008年的2.90.00版本,作者就实现了TkbmMWThreadList,之后在kbmMW 4.40.00版本中,作者使用了Delphi的新特性,实现TkbmMWThreadList的泛型版本,用以实现线程安全的列表。要使用TkbmMWThreadList,首先要引用kbmMWGlobal单元。

然后我们来看看,如何用TkbmMWThreadList实现自己的列表来管理对象。

1.声明列表对象:

FConnectionList:TkbmMWThreadList<TConnectionDataModule>;

上面代码,用FConnectionList来管理TConnectionDataModule对象。

2.建立FConnectionList实例:

FConnectionList := TkbmMWThreadList<TConnectionDataModule>.Create;

3.向对象列表填加被管理的对象:

procedure TMainDatamodule.AddConnection(AConnectionDataModule: TConnectionDataModule);
var
lst:TList<TConnectionDataModule>;
begin
lst:=FConnectionList.BeginWrite;
try
lst.Add(AConnectionDataModule);
finally
FConnectionList.EndWrite;
end;
end;

调用AddConnection方法,增加一个被管理的对象:

...
AddConnection(cdm);
...

4.清空列表对象:

procedure TMainDatamodule.ClearAllConnection;
var
i:integer;
lst:TList<TConnectionDataModule>;
begin
lst:=FConnectionList.BeginWrite;
try
for i:=lst.Count- downto do
lst.Items[i].Free;
lst.Clear;
finally
FConnectionList.EndWrite;
end;
end;

上面代码,清空列表对象中的所有被管理的对象。

5.读取列表中的被管理的对象:

function TMainDatamodule.GetConnectionPool(AConnectionName: string): TConnectionDataModule;
var
i: Integer;
lst:TList<TConnectionDataModule>;
begin
Result := nil;
lst:=FConnectionList.BeginRead;
try
for i := to lst.Count - do
begin
if lst.Items[i].Name = AConnectionName then
begin
Result := lst.Items[i];
Break;
end;
end;
finally
FConnectionList.EndRead;
end;
end;

基本实现方法,都写了,可以在线程中安全的使用FConnectionList对象,对TConnectionDataModule进行管理!

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,492
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,494
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,295