Delphi 6. 如何在无连接时设置 idTelnet.Connect 提前 return 超时

Delphi 6. How to set idTelnet.Connect timeout for early return on no connection

调用 idTelnet.Connect 通常会立即连接到远程设备。
但是,
如果远程设备没有响应, 那么对 idTelnet.Connect 的调用不会 return.
它等待来自远程设备的响应。

这会挂起整个应用程序。

如何设置超时,以便 idTelnet.Connect returns 在 nn 毫秒内
不管是否建立连接?

if the remote device does not respond, then the call to idTelnet.Connect does not return.

是的,它最终会。

It waits for a response from the remote device.

或者直到 OS 最终放弃并使连接失败,报告 Indy 将作为异常引发的错误。

This hangs the entire application.

这意味着您在 UI 主线程的上下文中调用 Connect(),您一开始就不应该这样做。如果您必须这样做,请至少在您的 MainForm 上放置一个 TIdAntiFreeze 组件(并准备好处理可能引入的任何重入后果)。否则,将您的套接字代码移至单独的工作线程。

How can I set a timeout so that idTelnet.Connect returns within nn ms regardless of whether a connection has been established or not?

您没有说明您使用的是哪个版本的 Indy。 Delphi 6很老了。如果您使用的是随附的 Indy 版本,那么您使用的是 Indy 8 或 9。Connect() 在 Indy 8 中根本没有超时功能。在 Indy 9 中,Connect() 有一个可选的ATimeout 参数。在 Indy 10 中,ATimeout 参数被替换为新的 ConnectTimeout 属性.