Home > Back-end >  Try to thread to execute SQL queries, but always abnormal. Please help me to look at the code, thank
Try to thread to execute SQL queries, but always abnormal. Please help me to look at the code, thank

Time:10-17

Purpose is to want to in the process of executing SQL query, allows the user to click on the "end" instead of the query, searched some code on the net, oneself do a change on the test, not the result, the following is my test code:

Three buttons on TForm1:
Button11, used to establish a new thread, and point to execute an SQL query functions;
Button22, SQL - point used to start this exception error;
Button33 for termination of the query - haven't tried to this step,

//-- -- -- -- -- -- -- -- -- thread entry function
The function MyThreadFun (p: Pointer) : Integer; Stdcall;
Var
Quy: TADOQuery;
The begin
With TADOQuery. Create (application) do
The begin
Connection:=ADOConnection1;//ADOConnection1 is entering the system of public connection.
The close;
SQL. The Clear;
SQL. The add (' exec PRC_TEST ');
The main content of PRC_TEST//the server process is: WAITFOR DELAY '2:00'
The Open;
end;

Result:=0;
end;

//-- -- -- -- -- -- -- -- -- to build and hung thread
Procedure TForm1. Button11Click (Sender: TObject);
Var ID: DWORD;
The begin
HThread:=CreateThread (nil, 0, @ MyThreadFun, nil, CREATE_SUSPENDED, ID);
//for the Enabled:=False;
end;

//-- -- -- -- -- -- -- -- -- wake up and continue to thread, start the query
Procedure TForm1. Button22Click (Sender: TObject);
The begin
ResumeThread (hThread);
end;

//-- -- -- -- -- -- -- -- -- cancel the SQL query
Procedure TForm1. Button33Click (Sender: TObject);
The begin
ADOConnection1. Cancel;;
end;



Please everyone to give guidance, thank you!

CodePudding user response:

CoInitialize (nil);
Try
.
The finally
CoUninitialize;
end;
Using ADO to connect in a thread need to join the above statement,

CodePudding user response:

See code feel can perform once, and then can't perform

CodePudding user response:

refer to the second floor xabcxyz response:
see code feel can perform once, then cannot perform


I think so, too, execute the stored procedure can't stop it...

CodePudding user response:

reference feiba7288 reply: 3/f
refer to the second floor xabcxyz response:
Think can perform a code, and then can't perform the

I think so, too, execute the stored procedure can't stop it...



ADOConnection1. Cancel;
This is used to terminate the process execution, should be ok,

CodePudding user response:

reference 1st floor Oraclers response:
CoInitialize (nil);
Try
.
The finally
CoUninitialize;
end;
Using ADO to connect in a thread need to join the above statement,


Thank you for your answer,
As you say, I so, still wrong:
The function MyThreadFun (p: Pointer) : Integer; Stdcall;
The begin
Try
Coinitialize (nil);
With TADOQuery. Create (application) do
The begin
Connection:=ADOConnection1;
The close;
SQL. The Clear;
SQL. The add (' exec PRC0300 ');
The Open;
end;
The finally
CoUninitialize;
end;
end;

CodePudding user response:

You may have a problem, so you want to perform a storage process, the following with the OPEN, might be wrong, suggest you change the SQL statement to: first
SQL. The Add (' select * from t1);//to find a record number of little table, look to whether make a mistake,

CodePudding user response:

refer to 6th floor Oraclers response:
you such use may have a problem, you want to execute a stored procedure, the following with the OPEN again, might be wrong, I suggest you to change the SQL statement to:
SQL. The Add (' select * from t1);//to find a record number of little table, look to whether errors,


Records of less, soon finished performing, I have no chance to stop it, so they use a fixed straight in the process of waiting,

CodePudding user response:

Have no friends have done this thing...

CodePudding user response:

ADOConnection1. Cancel; I don't just said that the query results, the query will still on the server to!
CreateThreadchuan do you use to create a thread must use global variables IsMultiThread set to true, or to create a thread with BeginThread,
Of course it is best to inherit TThread to do,

CodePudding user response:

references 9 f sololie response:
ADOConnection1. Cancel. I don't just said that the query results, the query will still on the server to!
CreateThreadchuan do you use to create a thread must use global variables IsMultiThread set to true, or to create a thread with BeginThread,
Of course it is best to inherit TThread,

Hello, thank you for your answer,
Can help to write a piece of code? Thank you all the!

CodePudding user response:

You don't use a thread to test your first this stored procedure can return correct results, if there is abnormal, abnormal hint is given
  • Related