如果有客户在连接,TIdTCPServer组建如果强行将Active设为False,会出现"Thread Terminate Timeout"错误,这时如何关闭连接。

解决方案 »

  1.   

    修改Indy源码即可避免此类问题
      

  2.   

    服务端的线程列表是线程安全的,我怎么试都会提示:"Thread Terminate Timeout",能给个示例吗?
      

  3.   

    我也碰到这个问题,给你一段代码
    在TCPServer.Active := false前
    加一个函数
    procedure TfrmIndy.btnDisconnectAll;
    var
      List: TList;
      I: Integer;
    begin
      List := TCPServer.Threads.LockList;
      try
        for I := 0 to List.Count - 1 do
        begin
          try
            //if TCPServer.Checked then
            //begin
              //TIdPeerThread(List.Items[I]).Connection.WriteLn('#');
            //end;
            TIdPeerThread(List.Items[I]).Connection.Disconnect;
          except
            //on E: EIdNotConnected do
            //begin
            //  //Dont worry about this error
            //end;
            on E: Exception do
            begin
              form1.spSkinMemo1.Lines.Add('Exception (' +datetimetostr(Now) + ') [' + E.ClassName + ']: ' + E.Message);
              form1.spSkinMemo1.Lines.Add('The thread has been stopped');
              TIdPeerThread(List.Items[I]).Stop;
            end; //on
          end; //try
        end; //for
      finally
        TCPServer.Threads.UnlockList;
      end;
    end;在直接运行EXE时不会报错,但在程序运行时还是会保超时的错。
      

  4.   

    indy 就是有很多烦人的事,我都改回 tcpserver和client了
      

  5.   

    procedure Tfrm_ServerMain.BitBtn2Click(Sender: TObject);       //强制关闭所有客户连接线程,可以把下面代码放到服务器端onclose事件中去
    var
      i,j : integer;
    begin
      lbxUser.Items.Clear;
      j := MyList.LockList.Count;
      MyList.UnlockList;
      if j > 0 then
      begin
        for i := 0 to j -1 do
        begin
          TIdPeerThread(MyList.LockList.Items[i]).Connection.Disconnect;
          MyList.UnlockList;
        end;
      end; 
    end;