匿名方法实现多线程同步到主线程执行
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
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
相关文章推荐
- 匿名方法实现多线程同步到主线程执行
- 利用动态代理+注解 实现子线程中执行方法
- Java实现等待所有子线程结束后再执行一段代码的方法
- Java 多线程实现的三种方法,附两个线程执行不同的输出
- 一种利用线程池线程执行任务, 并能够结束超时任务的方法 (.NET实现)
- Java_基础—多线程(匿名内部类实现线程的两种方式和设置名字的方法)
- Thread.join()方法实现main()方法等待所有子线程执行完成[base jdk8]
- 简单快速实现使用线程后台执行作业的方法
- Java线程暂停一段时间之后继续执行的实现方法
- (转载)线程定时执行的两种实现方法
- C#中Join()方法实现线程的顺序执行
- 多线程(线程间通信-多生产者多消费者问题-JDK1.5解决办法-范例),停止线程,线程中方法的区别,匿名内部类实现多线程,线程总结
- C#线程 在某一时间内,只有N个线程在并发执行,其余都在队列中的实现
- 借用.net framework的string.Fromat(...),实现一个执行参数化SQL的方法
- CODE:使用匿名方法,让worker线程中更新UI控件的代码更优雅
- 线程的状态、实现与启动、实现同步的方法
- 在Java中实现线程的两种方法
- Servlet执行时一般实现哪几个方法?
- (八)线程--借助 封装类 实现“线程调用带参方法”(示例下载)
- 借助封装类实现线程调用带参方法