多线程 mssql數據庫問題 |
|
wanliang
一般會員 發表:1 回覆:0 積分:0 註冊:2006-10-11 發送簡訊給我 |
我通過win API CreateThread創建一個進程,調用一個function.如果這個function中有两个ADOQuery,關閉窗口不能安全退齣,什么数据异步错误..請教這是為什么,聽說要兩個session ,我分別連接的是兩個TADOConnection(隻是兩個TADOConnection都連同一數據庫)兩個TADOConnection是兩個session 嗎?
unit Unit2; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, DB, ADODB, StdCtrls, Grids, DBGrids; type TForm2 = class(TForm) DBGrid1: TDBGrid; Button2: TButton; Button3: TButton; ADOConnection1: TADOConnection; ADOQueryJB: TADOQuery; ADOQuery1: TADOQuery; ADOConnection2: TADOConnection; Label1: TLabel; procedure Button2Click(Sender: TObject); procedure Button3Click(Sender: TObject); procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean); private hThreadHandle1:THandle; public { Public declarations } end; var Form2: TForm2; implementation uses Unit1; {$R *.dfm} function aaa:Integer;stdcall; var I:integer; jb,jb1,sz1:string; begin for I := 1 to 100000 do begin ADOQueryJB.Close; ADOQueryJB.SQL.Clear; ADOQueryJB.SQL.Add('select * from temp0 '); ADOQueryJB.Open; ADOQuery1.SQL.Clear; ADOQuery1.SQL.Add('select * from temp1'); ADOQuery1.Open; end; end; procedure TForm2.Button2Click(Sender: TObject); var dwThreadID:Cardinal; ThreadID:DWord; begin if hThreadHandle1 = 0 then begin hThreadHandle1:=CreateThread(nil,0,@aaa,nil,0,dwThreadID); end else begin TerminateThread(hThreadHandle1,0); CloseHandle(hThreadHandle1); hThreadHandle1 := 0; end; end; procedure TForm2.Button3Click(Sender: TObject); begin if hThreadHandle1 <> 0 then begin TerminateThread(hThreadHandle1,0); CloseHandle(hThreadHandle1); hThreadHandle1 := 0; end; end; procedure TForm2.FormCloseQuery(Sender: TObject; var CanClose: Boolean); begin CanClose:=false; //结束线程 if hThreadHandle1 <> 0 then begin TerminateThread(hThreadHandle1,0); CloseHandle(hThreadHandle1); hThreadHandle1 := 0; end; CanClose:=true; end; end. |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |