您的位置:首页 > 其它

匿名方法实现多线程同步到主线程执行

2016-09-17 00:22 369 查看
高版本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;
http://www.cnblogs.com/hnxxcxg/p/5670720.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: