从外部破坏 Indy 上下文

Destroying Indy Context from the outside

从外部破坏TIdContext后代的正确方法是什么?我想关闭一个特定的(挂起的)连接并销毁它的上下文。

说,我正在从 TIdCustomTCPServer.Contexts 中获取一个元素,并且...我猜调用 Free 是不够的。我只是想避免任何陷阱。有时 Indy 根本不直观。

您可能想使用这样的东西。不释放任何上下文

var
  i: Integer;
  list: TList;
begin
  list := IdTCPServer1.Contexts.LockList;
  try
    for i := 0 to list.Count-1 do
    begin
      try
        TIdContext(list.Items[i]).Connection.Disconnect;
       except

       end;
    end;
  finally
    IdTCPServer1.Contexts.UnlockList;
  end;
end;

What's the proper way of destroying TIdContext descendant from the outside?

根本不做。您不拥有 Context 对象,您无权销毁它们。服务器拥有它们,它将为您管理它们。

I want to close a particular (hanging) connection and destroy its context.

上下文在其拥有的线程终止时自动销毁。默认情况下,该线程在关联的套接字关闭时终止。确保您的事件处理程序没有阻止异常,除非您手动调用 Disconnect()

Say, I'm taking an element from TIdCustomTCPServer.Contexts and... Calling Free is not enough I guess.

请不要直接销毁上下文。但是,您 可以 调用 Context.Connection.Disconnect()