procedure TForm1.N3Click(Sender: TObject); 
var 
j :integer; 
begin 
  IdTCPServer1.Threads.UnlockList; 
  for j := 0 to IdTCPServer1.Threads.LockList.Count-1 do 
  begin 
  TIdPeerThread(IdTCPServer1.Threads.LockList.Items[j]).Connection.DisconnectSocket; 
      IdTCPServer1.Threads.UnlockList; 
  end; 
  IdTCPServer1.Threads.LockList.Clear; 
IdTCPServer1.Threads.UnlockList; 
  IdTCPServer1.Active:=false; 
  N3.Enabled := False; 
  N2.Enabled := True; 
  RzMemo1.Lines.Add('服务已停止。'); 
end; 以上代码经过多次实验,在有客户端连接的状态下关闭服务没有问题,但在刚启动服务马上关闭服务时会出现屏幕假死,问题主要出现在IdTCPServer1.Threads.LockList.Count这个上,请各位大侠如何解决? 用的D7 自带的indy

解决方案 »

  1.   

    建议用RemObject组件,通信层的事你几乎都不用去管了
      

  2.   

    你一直用UnlockList干什么,LockList/UnlockList要成对使用。
      

  3.   

    为什么要搞那么复杂去呢?IdTCPServer1.Active:=false不行吗?我想delphi不至于留这个一个BUG到现在吧?在TIdTCPServer.SetActive函数里面,有这么两个调用,我想已经可以达到你的要求了
     
     TerminateListenerThreads; 
     TerminateAllThreads;可参考 http://topic.csdn.net/u/20090815/11/752660bf-9b0a-474f-b45e-0426d97ce874.html