请教多线程问题(50分)

  • 主题发起人 主题发起人 gdzscj
  • 开始时间 开始时间
G

gdzscj

Unregistered / Unconfirmed
GUEST, unregistred user!
我做了个小程序如下:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, DB, ADODB, StdCtrls, Grids, DBGrids;
type
TForm1 = class(TForm)
DBGrid1: TDBGrid;
Button1: TButton;
DataSource1: TDataSource;
ADOQuery1: TADOQuery;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

tquerythread=class(TThread)
private
fquery:Tadoquery;
fdatasource:Tdatasource;
procedure connectdatasource;
protected
procedure execute;
override;
public
constructor create(query:Tadoquery;datasource:Tdatasource);virtual;
end;

var
Form1: TForm1;
implementation
{$R *.dfm}
constructor Tquerythread.create(query:Tadoquery;datasource:Tdatasource);
begin
inherited create(false);
fquery:=query;
freeonterminate:=true;
end;

procedure Tquerythread.execute;
begin
fquery.Open;
connectdatasource;
end;

procedure Tquerythread.connectdatasource;
begin
fdatasource.dataset:=fquery;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
Tquerythread.create(adoquery1,datasource1);
end;

end.

但运行时出错,提示:
project project1.exe raised exception class EoleExeption with message '尚未调用Coinitialize'。process stop,user step or run to continue.
如何才能正确运行呢?
 
uses activex;
initialization
activex.CoInitialize(nil);
finalization
activex.CoUninitialize;
 
TQueyrthread不在主线程中运行,COM库未初始化,所以应该修改如下(供参考)
procedure Tquerythread.execute;
begin
Coinitialize(nil);
try
fquery.Open;
connectdatasource;
finally
CoUninitialize;
end;
end
 
多人接受答案了。
 
后退
顶部