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