匿名方法实现多线程同步到主线程执行
2016-07-14 15:58
288 查看
高版本DELPHI提供的匿名方法,如果使用的好,可有效地节省代码。
procedure TCMServerForm.CMServerTransportDisconnectEvent(Event: TDSTCPDisconnectEventObject);
var
Index: Integer;
begin
if (FConnections = nil) or (Event.Connection = nil) then
Exit;
// 进入临界保护
System.TMonitor.Enter(FConnections);
try
FConnections.Remove(TIdTCPConnection(Event.Connection));
// 匿名方法同步到主线程执行
TThread.Synchronize(nil, procedure
begin
//update the connection list box, removing the connection that was just closed
Index := ConnectionsList.Items.IndexOfObject(Event.Connection);
if Index > -1 then
begin
ConnectionsList.Items.Delete(Index);
if ConnectionsList.SelCount = 0 then
SessionIdList.ClearSelection;
end;
end);
finally
// 退出临界保护
System.TMonitor.Exit(FConnections);
end;
end;
procedure TCMServerForm.CMServerTransportDisconnectEvent(Event: TDSTCPDisconnectEventObject);
var
Index: Integer;
begin
if (FConnections = nil) or (Event.Connection = nil) then
Exit;
// 进入临界保护
System.TMonitor.Enter(FConnections);
try
FConnections.Remove(TIdTCPConnection(Event.Connection));
// 匿名方法同步到主线程执行
TThread.Synchronize(nil, procedure
begin
//update the connection list box, removing the connection that was just closed
Index := ConnectionsList.Items.IndexOfObject(Event.Connection);
if Index > -1 then
begin
ConnectionsList.Items.Delete(Index);
if ConnectionsList.SelCount = 0 then
SessionIdList.ClearSelection;
end;
end);
finally
// 退出临界保护
System.TMonitor.Exit(FConnections);
end;
end;
相关文章推荐
- 不良资产处置(五):不同国家不良资产处置案例分析
- soapui 常用包和函数
- session入库
- 【NOIP2016A组模拟7.13】搬运干草捆
- LinkedList基本用法
- 项目:个人主页=>用户头像(以及头像裁剪)+用户信息(点点点,查看更多)+滚动加载
- Intent 各种跳转
- Ubuntu会怎么玩物联网平台
- 剑指offer-5-面试31:连续子数组的最大和(时间效率)
- httpd.conf服务器配置简单介绍
- 静态路由的配置方法
- Linux中查看CPU信息【转】
- windows安装rsync
- centos python 安装 readability
- 利用双缓冲队列来减少锁的竞争
- JVM的GC日志分析
- CNN的训练图像与测试图像不一致的多尺度问题
- javascript prototype原型继承
- 如何通过Haystack建立pdf文档的全文索引
- 基于 Selenium WebDriver 的 Web 应用自动化测试