TIdTCPServer.Active := False的研究
http://blog.csdn.net/jadeluo/article/details/3958423
设置TIdTCPServer的Active为False的这个操作存在着一些问题:
1. 在设置Active为False时, 将结束所有已连接的客户端线程(PeerThread), 这个结束操作是有超时检测的(超时的时长可以通过TerminateWaitTime属性进行设置)。如果超时, 则抛出超时异常,这个异常在Delphi的IDE环境中无法屏蔽,给调试程序带来很多不便。而且,出现超时异常时,TIdTCPServer并未正常关闭,还得再次设置Active为False。
2. 从设置Active为False开始到所有PeerThread线程结束或者抛出超时异常,主进程将阻塞,主进程的界面将无响应。
这些问题都是让人不能接受的。
研究了一下Indy的源码,写了下面这段代码,来解决TIdTCPServer.Active := False的问题。
- uses IdThreadSafe; //需要引用IdThreadSafe单元
- ......
- var
- AList: TList;
- iLoop: Integer;
- begin
- if Assigned(IdTCPServer1.Threads) then
- begin
- AList := IdTCPServer1.Threads.LockList;
- try
- for iLoop := 0 to AList.Count -1 do
- begin
- TIdPeerThread(AList.Items[iLoop]).Connection.Disconnect;
- TIdPeerThread(AList.Items[iLoop]).Stop;
- end;
- finally
- IdTCPServer1.Threads.UnlockList;
- end;
- while not TIdThreadSafeList(IdTCPServer1.Threads).IsCountLessThan(1) do
- begin
- Application.ProcessMessages;
- Sleep(100);
- end;
- end;
- IdTCPServer1.Active := False;
- end;
delphi lazarus opengl
网页操作自动化, 图像分析破解,游戏开发