Cancel ADO connection attempt to connect?
I have a TADOConnection
thread inside. In case of failure to connect to the database (timeout) when closing the application, the thread persists and it takes a while until the attempt completes before my application can close. I do not want to shorten the connection time of the connection, this is not a problem. Is there a way to forcefully stop trying to connect?
TADOConnection
connects at the start of a thread and automatically reconnects until successful. Then, after closing the application, if the database is not connected, the thread hangs until the connection attempt completes (timed out).
EDIT
This is an example of how the thread works:
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;
(Tried to include only something to the question)
source to share
The first thing that comes to mind is to reduce the connection timeout. Why don't you want it? And why do you want to establish a connection when you close the application? Especially when you prefer to interrupt it when it takes longer than expected, why bother connecting at all? It looks like we could find out more information about the backstory.
For a special case that you really want, assuming it connects quickly, and when this problem only applies to killing the application, I suggest not waiting for the thread to end. Just don't free it, terminate the application, and let Windows kill the process, including all its threads.
In case the connection is indeed successful, then this approach can backfire, so signal your main thread when the thread connects and defer its completion while you wait for the thread. You may need another timeout for this.
Edit:
I am assuming the event OnWillConnect
will appear every time you try to connect. Try to return EventStatus := esCancel
to your handler.
source to share