简体   繁体   English

如何在XE2中中止TSQLConnection连接尝试?

[英]How to abort a TSQLConnection connect attempt in XE2?

Using the TSQLConnection component in Delphi XE2 to connect to a DataSnap server. 使用Delphi XE2中的TSQLConnection组件连接到DataSnap服务器。

My problem is that I would like to abort the connection attempt if we're unable to connect after 3 seconds, however setting the ConnectTimeout property of the driver doesn't seem to have any impact (it still waits around 20 seconds before giving up). 我的问题是,如果我们在3秒后无法连接,我想中止连接尝试,但是设置驱动程序的ConnectTimeout属性似乎没有任何影响(它在放弃之前仍然等待大约20秒) 。

I'm running this inside a thread and so I could post a message to the thread (how whether the thread message queue is serviced depends, I suppose, on how this blocking call was created). 我在一个线程中运行它,所以我可以向线程发布一条消息(线程消息队列是否被服务取决于我认为这个阻塞调用是如何创建的)。 Even if my thread message handler ran, I'm unsure how I'd then interrupt the blocking the call to set the TSQLConnection.Connected property (which is set within the Execute method of the thread). 即使我的线程消息处理程序运行,我也不确定如何中断阻塞调用以设置TSQLConnection.Connected属性(在线程的Execute方法中设置)。

I'd prefer not call TerminateThread as this seems overkill and would leave (if I understand correctly) the memory allocated to the thread's stack. 我不想调用TerminateThread,因为这似乎有点过分,并且会留下(如果我理解的话)分配给线程堆栈的内存。 Any ideas on how to interrupt this connection process, or access the underlying Indy components and explicitly set the connection timeout there would be appreciated. 有关如何中断此连接过程,或访问基础Indy组件并明确设置连接超时的任何想法将不胜感激。

Thanks! 谢谢!

fConnection := TSQLConnection.Create(nil);
with fConnection do
begin
  DriverName := 'DataSnap';
  Params.Values['CommunicationProtocol'] := 'tcp/ip';
  Params.Values['DatasnapContext'] := 'datasnap/';
  Params.Values['HostName'] := '127.0.0.1';
  Params.Values['Port'] := '211';
  Params.Values['ConnectTimeout'] := '3000';
  KeepConnection := true;
  LoginPrompt := true;
end;

// Where it blocks for up to 20 seconds (if host unavailable)
fConnection.Connected := True;

I think about this and if ConnectTimeout not work then you can solve this in another way 我想到这一点,如果ConnectTimeout不起作用,那么你可以用另一种方式解决这个问题

first before connect try to ping dest host by eg Indy component (if this is not the localhost ;-)) or better try telnet service port (also by Indy) - i suppose that this is the simplest think what you can do now without killing the thread 首先在连接尝试通过例如Indy组件ping目标主机(如果这不是本地主机;-))或更好的尝试telnet服务端口(也由Indy) - 我想这是最简单的认为你现在可以做什么而不杀线程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM