现象如下:如果在多线程中使用Tquery,可以正常打开查询,返回结果,如果相同的代码换成TADOQuery则在打开数据库时报错,错误是: Project Project1.exe raised exception class EOleException with message '尚未调用 coinitialize.'。process stopped.use step or Run to continue.
瞎猜一下:Ado是基于com的,下面msdn上的一段关于CoInitialize的说明或许会有用。 Initializes the COM library on the current apartment and identifies the concurrency model as single-thread apartment (STA). Applications must initialize the COM library before they can call COM library functions other than CoGetMalloc and memory allocation functions.
to: richardsong(白玉老虎) 我的帖子也被移过,还有别人的也被移过,CSDN的管理员他们也可以移。 你的帖子是我移的,你向Delphi搬救兵,所以我移到Delphi来了。 呵呵,就此打住吧。不争了,不争了。
我试了一下,在主线程中先随便操作一下AdoQuery1(比如AdoQuery1.SQL.Text:='...')就能解决你的问题,不过我相信以后还会有其它问题。以下文字你应该见过:Methods and properties of objects in VCL can only be used in a method called using Synchronize. 其实ADO组件本身支持异步操作的,在ExecuteOptions属性中设置,我没研究过。 有纪录才能关闭的问题,安装ADO组件补丁(VCL,不是ADO本身)应该就能解决。
做以下操作之前,先做
adoQuery.Close;
adoquery.SQL.Text:='select sum([Interval]) from Recordtab ';
adoquery.Open;//在此出错
Synchronize(你的程序名);
ok
先通过了,然后再想别的方法。
看来Delphi的ado还是有问题。
Project Project1.exe raised exception class EOleException with message '尚未调用
coinitialize.'。process stopped.use step or Run to continue.
我明天再来,可以联系[email protected]
Initializes the COM library on the current apartment and identifies the concurrency model as single-thread apartment (STA). Applications must initialize the COM library before they can call COM library functions other than CoGetMalloc and memory allocation functions.
TADOQuery1.RecordSet.Close();
再试一试。
也可以这样
在线程中调用其他单元的函数或过程来执行
注意vcl控件的执行期间调用问题
constructor ttest.Create(ADOQ: TADOQuery);
begin
ADOQuery := TADOQuery.Create(nil);
ADOQuery.Asign(ADOQ);
inherited create(false);
end;
一个query关闭再执行另外一个啊,否则服务器会被你拖死的。
是多线程耶!怎么关闭在执行另一个?
再者说,我又不是斑竹,我说了也不算,他自己跑过去的,我有什么办法?
是我的帖子吗?
还有:我没说你没用过多线程,我从来不挖苦别人,嘲笑别人或者贬低别人,这个良好作风Delphi里的弟兄可以作证,我们斑竹也可以作证的。老兄不要随便冤枉好人。
ADOQuery:=ADOQ;
inherited create(false);
这两句调过来试试看吧,我一向写新类都是先写第二句的,因为要有新类的实体,必定要先建立吗!?
如果还不行,那应该是戴妃与M$不和了.
richardsong(白玉老虎)
我的帖子也被移过,还有别人的也被移过,CSDN的管理员他们也可以移。
你的帖子是我移的,你向Delphi搬救兵,所以我移到Delphi来了。
呵呵,就此打住吧。不争了,不争了。
其实ADO组件本身支持异步操作的,在ExecuteOptions属性中设置,我没研究过。
有纪录才能关闭的问题,安装ADO组件补丁(VCL,不是ADO本身)应该就能解决。
Ok,就此打住。
VCL的相当一部分代码是线程不安全的,具体的说,大多数由TComponent所派生的类与线程不兼容,会导致一些错误。而Synchronize能解决这个问题,当然效率会低一些。但却是必须的。