我也碰到这个问题,给你一段代码 在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时不会报错,但在程序运行时还是会保超时的错。
indy 就是有很多烦人的事,我都改回 tcpserver和client了
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;
在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时不会报错,但在程序运行时还是会保超时的错。
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;