Delphi Pages Forums  

Go Back   Delphi Pages Forums > Delphi Forum > General

Lost Password?

Reply
 
Thread Tools Display Modes
  #1  
Old 09-28-2012, 11:00 AM
rlb rlb is offline
Junior Member
 
Join Date: Sep 2012
Posts: 2
Default thread with ADO, ADO not working

Hi,

I'm using Delphi 5 and SQL Server.

I've tried creating a thread to run a search process so it can be cancelled if an appropriate result is found before the last stage is ran.

I've set up an ADOConnactiona and ADOQuery within the thread.
I'm using an ADOQuery to run the different search stages.
I've placed the ADO code within CoInitialize(nil) and CoUninitialize calls.

When I debug the application the ado query setup and execute code lines are ran twice (why?) and on the 2nd run on the line (FTmpResultsAdoQry.ExecSQL then the following errors appear:

Error EOleException with message 'operation is not allowed when the object is open'.

Error EOleException with message 'operation cannot be performed while executing asynchronously '.

Heres a code snippet of the thread execute code
procedure TSearchThread.Execute;
var
i: Integer;
begin
CoInitialize(nil);
try
FAdoConnection := TADOConnection.Create(nil);
try
FAdoConnection.LoginPrompt := False;
FAdoConnection.ConnectionString := ConnStr;
FAdoConnection.Open;
FTmpResultsAdoQry := TAdoQuery.Create(nil);
try
FTmpResultsAdoQry.Connection := FAdoConnection;
for i := 1 to 4 do
begin
FTmpResultsAdoQry.Close;
FTmpResultsAdoQry.SQL.Text := 'exec prem_find @stage='+IntToStr(i);
FTmpResultsAdoQry.ExecSQL; //*** Fails on this line ***
FTmpResultsAdoQry.Close;

//Synchronize(UpdateResults);
end;
finally
if FTmpResultsAdoQry.Active then
FTmpResultsAdoQry.Close;
FTmpResultsAdoQry.Free;
end;
finally
if FAdoConnection.Connected then
FAdoConnection.Close;
FAdoConnection.Free;
end;
finally
CoUninitialize();
end;
end;

Any ideas why it would be trying the run the following code twice, resulting in the errors mentioned above.
FTmpResultsAdoQry.Connection := FAdoConnection;
for i := 1 to 4 do
begin
FTmpResultsAdoQry.Close;
FTmpResultsAdoQry.SQL.Text := 'exec prem_find @stage='+IntToStr(i);
FTmpResultsAdoQry.ExecSQL;

Any ideas much appreciated.
R
Reply With Quote
  #2  
Old 10-03-2012, 11:17 AM
rlb rlb is offline
Junior Member
 
Join Date: Sep 2012
Posts: 2
Default

It was a silly error that caused the problem.

I created the thread with the following code:

//Start code snippet
with TSearchThread.Create(
FADOConnection.ConnectionString, FADOConnection.Provider, FADOConnection) do
OnTerminate := ThreadDone;
SearchBtn.Enabled := False;
try
SearchThread.Execute;
finally
SearchThread.Terminate;
end;
//End code snippet

The call to SearchThread.Execute and SearchThread.Terminate were unnecessary. Calling SearchThread.Execute started a second thread running giving the unusual debug results. Both lines have been removed and the thread now runs ok.
R.
Reply With Quote
Reply

Tags
ado, adoquery, thread

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT. The time now is 04:49 PM.


Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2019, vBulletin Solutions, Inc.