ADO 使用线程时出错 ( 积分: 50 )

  • 主题发起人 主题发起人 caizfom
  • 开始时间 开始时间
C

caizfom

Unregistered / Unconfirmed
GUEST, unregistred user!
今天看了一个程序用BDE写的是这样(在一个线程代码里)<br>procedure&nbsp;MYThread.Execute;<br>begin<br>&nbsp;&nbsp;FQuery.Open;<br>&nbsp;&nbsp;FDataSource.DataSet&nbsp;:=&nbsp;FQuery;<br>end;<br>end;<br>constructor&nbsp;MYThread.Create(Q:&nbsp;TQuery;D:TDataSource);<br>begin<br>&nbsp;&nbsp;inherited&nbsp;Create(True);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;create&nbsp;suspended&nbsp;thread<br>&nbsp;&nbsp;FQuery&nbsp;:=&nbsp;Q;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;set&nbsp;parameters<br>&nbsp;&nbsp;FDataSource&nbsp;:=&nbsp;D;<br>&nbsp;&nbsp;FreeOnTerminate&nbsp;:=&nbsp;True;<br>&nbsp;&nbsp;Resume;<br>end;<br>以上代码可以运行并不会报错,但是为什么当我用上ADO控件把<br>constructor&nbsp;MYThread.Create(Q:&nbsp;TQuery;D:TDataSource);<br>改成<br>constructor&nbsp;MYThread.Create(Q:&nbsp;TADOQuery;D:TDataSource);<br>时候就出错了!<br>报&nbsp;&nbsp;--尚未调用Coinitialize&nbsp;的错误,后来我看了别人些方法在后面加了<br>initialization&nbsp;<br>&nbsp;CoInitialize(nil);&nbsp;<br>finalization&nbsp;<br>&nbsp;CoUninitialize;<br>还是出错,谁知道的帮帮忙
 
//象下面写就可以了!&nbsp;原因我也不明白,&nbsp;估计是子线程有自己独立的栈空间的原因吧<br>procedure&nbsp;MYThread.Execute;<br>begin<br>&nbsp;&nbsp;CoInitialize(nil);<br>&nbsp;&nbsp;try<br>&nbsp;&nbsp;&nbsp;&nbsp;FQuery.Open;<br>&nbsp;&nbsp;&nbsp;&nbsp;FDataSource.DataSet&nbsp;:=&nbsp;FQuery;<br>&nbsp;finally<br>&nbsp;&nbsp;&nbsp;CoUninitialize;<br>&nbsp;end<br>end;
 
谢谢,KK2000&nbsp;按你的方法可以了,但是这样为什么用ADO的速度要比用BDE的速度慢很多,为什么呢!
 
嗯,在线程里头也要调CoInittialize和CoUninitialize<br>------------------------------------------------------<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;http://www.waibaoinfo.com&nbsp;外包信息网
 
后退
顶部