adoquery多线程查询,以下代码请指教(50分)

  • 主题发起人 主题发起人 tl_lyq
  • 开始时间 开始时间
T

tl_lyq

Unregistered / Unconfirmed
GUEST, unregistred user!
form1放有adoquery,通过调用直接传递至线程内查询, 现在有时候会报错.....<br><br>unit &nbsp;querythread;<br><br>type<br>&nbsp; TDatabaseThread = class(TThread)<br>&nbsp; private<br>&nbsp; &nbsp; FForm:tform;<br>&nbsp; &nbsp; FADOQuery:TADOQuery;<br>&nbsp; &nbsp; SQLString:string;<br>&nbsp; &nbsp; isresult:boolean;<br>&nbsp; protected<br>&nbsp; &nbsp; procedure Execute; override;<br>&nbsp; public<br>&nbsp; &nbsp; constructor Create(Form1:Tform;ADO1:TADOQuery;Sqlstr:String;isresult1:boolean); overload;<br>end;<br><br><br>constructor TDatabaseThread.Create(Form1:Tform;ADO1:TADOQuery;Sqlstr:String;isresult1:boolean);<br>begin<br>&nbsp; screen.Cursor :=crSQLWait;<br>&nbsp; FADOQuery:=ADO1;<br>&nbsp; FADOQuery.disablecontrols;<br>&nbsp; SQLString:=Sqlstr;<br>&nbsp; isresult:=isresult1;<br>&nbsp; inherited Create(False);<br>end;<br><br>procedure TDatabaseThread.Execute;<br>begin<br>&nbsp; try<br>&nbsp; &nbsp; CoInitialize(nil);<br>&nbsp; &nbsp; with FADOQuery do<br>&nbsp; &nbsp; if FADOQuery&lt;&gt;nil then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; Close;<br>&nbsp; &nbsp; &nbsp; &nbsp; CommandTimeout :=120000;<br>&nbsp; &nbsp; &nbsp; &nbsp; SQL.text:=SQLString;<br>&nbsp; &nbsp; &nbsp; &nbsp; if isresult then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; open else ExecSQL;<br>&nbsp; &nbsp; &nbsp; if Terminated then<br>&nbsp; &nbsp; &nbsp; &nbsp; exit;<br>&nbsp; &nbsp; end;<br>&nbsp; finally<br>&nbsp; &nbsp; &nbsp; screen.Cursor:=crDefault;<br>&nbsp; &nbsp; &nbsp; FADOQuery.enablecontrols;<br>&nbsp; &nbsp; &nbsp; CoUninitialize;<br>&nbsp; end;<br>end;<br><br><br>调用时<br><br>unit Unit1;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br>&nbsp; Dialogs, Grids, DBGrids, DB, ADODB, StdCtrls;<br><br>type<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; Button1: TButton;<br>&nbsp; &nbsp; ADOQuery1: TADOQuery;<br>&nbsp; &nbsp; DataSource1: TDataSource;<br>&nbsp; &nbsp; DBGrid1: TDBGrid;<br>&nbsp; &nbsp; procedure Button1Click(Sender: TObject);<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; end;<br><br>var<br>&nbsp; Form1: TForm1;<br><br>implementation<br>&nbsp;uses querythread;<br>{$R *.dfm}<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var sql:string;<br>begin<br>&nbsp; &nbsp;sql:='select * from table1';<br>&nbsp; &nbsp;TDatabaseThread.Create(Form1,adoquery1,sql,True);<br>end;<br><br>end.
 
线程内自已创建TADOQUERY试试吧。
 
这种写法有什么错误吗, 我想在form1 调用查询线程后,把form1 的dbgrid 和button1 禁用掉,查询完后再启用, 有什么好办法吗?
 
VCL组件多线程时要注意线程同步吧。
 
sychronize做一个同步就可以了。另外, 多线程在单机作用不大。
 
还请详细指教一下..
 
//with FADOQuery do<br>with Tadoquery.create(nil) do &nbsp;//在线程内创建吧。<br>begin<br>&nbsp; &nbsp; Close;<br>&nbsp; &nbsp; CommandTimeout :=120000;<br>&nbsp; &nbsp; SQL.text:=SQLString;<br>&nbsp; &nbsp; if isresult then<br>&nbsp; &nbsp; open else ExecSQL;<br>&nbsp; &nbsp; if Terminated then<br>&nbsp; &nbsp; &nbsp; exit;<br>&nbsp; &nbsp;Free;<br>end;
 
不行啊,我的线程放在dm公用模块中, 查询在具体的模块,调用时传递adoquery到dm的线程中,如果在线程里创建的话,如何返回查询结果呢,
 
不行啊,我的线程放在dm公用模块中, 查询在具体的模块,调用时传递adoquery到dm的线程中,如果在线程里创建的话,如何返回查询结果呢,
 
线程不安全的是Connection。
 
发现如果form中的adoquery连接dxdbgrid,执行线程查询,有时候会报错,换成dbgrideh则好了.
 
后退
顶部