【发布时间】:2013-01-19 14:40:01
【问题描述】:
我在一个线程中有一个TADOConnection。如果它无法连接到数据库(超时),则在关闭应用程序时,线程会被暂停,并且在尝试完成之前我的应用程序能够关闭需要一些时间。我不想减少连接的连接超时,这不是问题。有什么方法可以强制中止连接尝试?
TADOConnection 在线程执行开始时连接并自动重复重新连接直到成功。然后,在关闭应用程序时,如果数据库连接失败,线程将挂起,直到连接尝试完成(超时)。
编辑
这是线程如何工作的示例:
procedure TMyThread.Init;
begin
CoInitialize(nil);
FDB:= TADOConnection.Create(nil);
FDB.LoginPrompt:= False;
FDB.ConnectionTimeout:= 5;
FDB.ConnectOptions:= coAsyncConnect;
end;
procedure TMyThread.Uninit;
begin
if FDB.Connected then
FDB.Connected:= False;
FDB.Free;
CoUninitialize;
end;
function TMyThread.Reconnect: Boolean;
begin
Result:= False;
if FDB.Connected then
FDB.Connected:= False;
FDB.ConnectionString:= FConnectionString;
try
FDB.Connected:= True; //How to abort?
Result:= True;
except
on e: exception do begin
//MessageDlg(e.Message, mtError, [mbOK], 0);
FDB.Connected:= False;
Result:= False;
end;
end;
end;
procedure TMyThread.Process;
begin
if Reconnect then begin //Once connected, keep alive in loop
while FActive do begin
if Terminated then Break;
if not Connected then Break;
//Do Some Database Work
end;
end else begin
//Log connection failure
end;
end;
procedure TMyThread.Execute;
begin
while not Terminated do begin
if FActive then begin
Init; //CoInitialize, create DB, etc.
try
while (FActive) and (not Terminated) do begin
try
Process; //Actual processing procedure
except
on e: exception do begin
//Record error to log
end;
end;
end;
finally
Uninit; //CoUninitialize, destroy DB, etc.
end;
end;
end;
end;
(试图只包含与问题相关的内容)
【问题讨论】:
-
您可以使用异步连接并在连接过程中调用
TADOConnection.Cancel。我不确定是否有办法在同步模式下中断连接过程。 -
@TLama 谢谢,似乎合乎逻辑,所以我尝试了,将
ConnectOptions更改为coAsyncConnect并在Stop过程中添加FDB.Cancel(这会阻止线程中发生的任何事情)-没有运气:( -
当我调用
TADOConnection.Cancel时,线程仍然挂起,直到尝试完成,它在该行代码处暂停。 -
@TLama 你能用一个简单的例子来回答吗?我将编辑我的问题并在线程中放置一个如何使用数据库的示例。
-
@JerryDodge 我发现了同样的问题,但我必须找到solution for VB6(我们不能直接使用线程)。所以解决方案在“后台”使用定时器来取消连接。
标签: delphi timeout delphi-xe2 ado abort