D
DARKKENLE
Unregistered / Unconfirmed
GUEST, unregistred user!
各位大哥,小弟刚写了一个多线程的小东东,不过在运行的时候老是报“尚未调用COINITIALIZA”的错误,我在工程文件已经对COM初始化了
线程代码如下:
unit Unit2;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, DB, ADODB, ExtCtrls, StdCtrls, Grids, DBGrids, ComCtrls,unit3;
type
thread = class(TThread)
private
{ Private declarations }
protected
procedure Execute;
override;
public
constructor create(query:boolean);
end;
var flag:boolean;
TreeView1: TTreeView;
ADOQuery1: TADOQuery;
ADOQuery2: TADOQuery;
ADOQuery3: TADOQuery;
which:String;
implementation
{ Important: Methods and properties of objects in visual components can only be
used in a method called using Synchronize, for example,
Synchronize(UpdateCaption);
and UpdateCaption could look like,
procedure thread.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end;
}
{ thread }
constructor thread.create(query:boolean);
begin
inherited Create(query);
FreeOnTerminate:=true;
end;
procedure init;
var A,i,b,C,D,E:integer;
VAR p1:array of Ttreenode;
VAR P2:ARRAY OF TTREENODE;
VAR P3:ARRAY OF ttREENODE;
VAR NAME:STRING;
VAR P0:TTREENODE;
var sub_num,TELHEAD:String;
begin
if flag=true then
begin
P0:=TREEVIEW1.Items.AddFirst(NIL,'全区');
ADOQuery1.Close;
ADOQuery1.SQL.Clear;
ADOQuery1.SQL.Add('select a.area a from darkken_ye a group by a.area');
adoquery1.Active:=true;
adoquery1.Open;
I:=ADOQuery1.RecordCount;
setlength(p1,i);
for a:=0 to i-1do
begin
NAME:=ADOQuery1.FieldValues['A'];
ADOQuery2.SQL.Clear;
ADOQuery2.SQL.Add('select A.DEPT A from darkken_ye a WHERE A.AREA=:NAME GROUP BY A.DEPT');
ADOQuery2.Parameters.ParamByName('name').Value:=name;
adoquery2.Active:=true;
adoquery2.Open;
b:=ADOQuery2.RecordCount;
setlength(p2,b);
P1[a]:=TREEVIEW1.Items.AddChild(p0,NAME);
FOR C:=0 TO B-1do
begin
sub_num:=ADOQuery2.FieldValues['A'];
P2[C]:=TREEVIEW1.Items.AddChild(P1[A],SUB_NUM);
ADOQUERY3.SQL.Clear;
ADOQUERY3.SQL.Add('SELECT A.TEL_HEAD A FROM DARKKEN_YE A WHERE A.AREA=:NAME AND DEPT=EPT');
ADOQuery3.Parameters.ParamByName('name').Value:=name;
ADOQuery3.Parameters.ParamByName('DEPT').Value:=sub_num;
adoquery3.Active:=true;
adoquery3.Open;
D:=ADOQUERY3.RecordCount;
SETLENGTH(P3,D);
FOR E:=0 TO D-1do
begin
TELHEAD:=ADOQUERY3.FieldValues['A'];
P3[E]:=TREEVIEW1.Items.AddChild(P2[C],TELHEAD);
ADOQUERY3.Next;
end;
ADOQuery2.Next;
ADOQUERY3.Close;
end;
ADOQuery2.CLOSE;
ADOQuery1.Next;
end;
ADOQuery1.Close;
end;
end;
procedure thread.Execute;
begin
init;
end;
end.
线程代码如下:
unit Unit2;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, DB, ADODB, ExtCtrls, StdCtrls, Grids, DBGrids, ComCtrls,unit3;
type
thread = class(TThread)
private
{ Private declarations }
protected
procedure Execute;
override;
public
constructor create(query:boolean);
end;
var flag:boolean;
TreeView1: TTreeView;
ADOQuery1: TADOQuery;
ADOQuery2: TADOQuery;
ADOQuery3: TADOQuery;
which:String;
implementation
{ Important: Methods and properties of objects in visual components can only be
used in a method called using Synchronize, for example,
Synchronize(UpdateCaption);
and UpdateCaption could look like,
procedure thread.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end;
}
{ thread }
constructor thread.create(query:boolean);
begin
inherited Create(query);
FreeOnTerminate:=true;
end;
procedure init;
var A,i,b,C,D,E:integer;
VAR p1:array of Ttreenode;
VAR P2:ARRAY OF TTREENODE;
VAR P3:ARRAY OF ttREENODE;
VAR NAME:STRING;
VAR P0:TTREENODE;
var sub_num,TELHEAD:String;
begin
if flag=true then
begin
P0:=TREEVIEW1.Items.AddFirst(NIL,'全区');
ADOQuery1.Close;
ADOQuery1.SQL.Clear;
ADOQuery1.SQL.Add('select a.area a from darkken_ye a group by a.area');
adoquery1.Active:=true;
adoquery1.Open;
I:=ADOQuery1.RecordCount;
setlength(p1,i);
for a:=0 to i-1do
begin
NAME:=ADOQuery1.FieldValues['A'];
ADOQuery2.SQL.Clear;
ADOQuery2.SQL.Add('select A.DEPT A from darkken_ye a WHERE A.AREA=:NAME GROUP BY A.DEPT');
ADOQuery2.Parameters.ParamByName('name').Value:=name;
adoquery2.Active:=true;
adoquery2.Open;
b:=ADOQuery2.RecordCount;
setlength(p2,b);
P1[a]:=TREEVIEW1.Items.AddChild(p0,NAME);
FOR C:=0 TO B-1do
begin
sub_num:=ADOQuery2.FieldValues['A'];
P2[C]:=TREEVIEW1.Items.AddChild(P1[A],SUB_NUM);
ADOQUERY3.SQL.Clear;
ADOQUERY3.SQL.Add('SELECT A.TEL_HEAD A FROM DARKKEN_YE A WHERE A.AREA=:NAME AND DEPT=EPT');
ADOQuery3.Parameters.ParamByName('name').Value:=name;
ADOQuery3.Parameters.ParamByName('DEPT').Value:=sub_num;
adoquery3.Active:=true;
adoquery3.Open;
D:=ADOQUERY3.RecordCount;
SETLENGTH(P3,D);
FOR E:=0 TO D-1do
begin
TELHEAD:=ADOQUERY3.FieldValues['A'];
P3[E]:=TREEVIEW1.Items.AddChild(P2[C],TELHEAD);
ADOQUERY3.Next;
end;
ADOQuery2.Next;
ADOQUERY3.Close;
end;
ADOQuery2.CLOSE;
ADOQuery1.Next;
end;
ADOQuery1.Close;
end;
end;
procedure thread.Execute;
begin
init;
end;
end.