好的,下面是我的代码:
procedure TForm1.FormCreate(Sender: TObject);
begin
thread5:=Tm5_Thread.Create(false);
end;
--------------------------------------------------------------------------
unit Unit2;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
jpeg, ExtCtrls,OleCtrls, ShockwaveFlashObjects_TLB, ComCtrls, Grids,
DBGrids, Db, ADODB,shellapi, StdCtrls, Buttons,ActiveX;//多了引用单元,没管它
type
Tm5_Thread = class(TThread)
private
{ Private declarations }
protected
procedure Execute;
override;
public
procedure cj;
end;
implementation
{ Important: Methods and properties of objects in VCL can only be used in a
method called using Synchronize, for example,
Synchronize(UpdateCaption);
and UpdateCaption could look like,
procedure Tm5_Thread.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end;
}
{ Tm5_Thread }
uses unit1;
//构造树形结构---------------------------------
procedure Tm5_Thread.Execute;
type
TAreaID = record
ID: String[20];
Len: Integer;
end;
PAreaID = ^TAreaID;
var tmpNode:TtreeNode;
AreaData:^TAreaID;
i,j:integer;
begin
//exit;
CoInitialize(nil);
ADOQuery_s:=TADOQuery.Create(self);
ADOQuery_s.Connection:=adoconn;
with ADOQuery_s do
begin
Close;
Sql.Clear ;
SQL.Add('SELECT aname,acode FROM m5_code where (LEN(Acode) = :AreaLen)');
SQL.Add('order by Acode');
Parameters.ParamByName('AreaLen').value := 2;
Open;
First;
end;
with ADOQuery_sdo
begin
while not(Eof)do
begin
tmpNode := Tree_m5.Items.Add(nil, FieldByName('Aname').AsString);
new(AreaData);
AreaData.ID := FieldByName('Acode').AsString;
AreaData.Len := 2;
tmpNode.Data := AreaData;
Next();
end;
Close();
for i := 2 to 5do
begin
Parameters.ParamByName('AreaLen').value:= i * 2;
Open();
while not(Eof)do
begin
tmpNode := nil;
for j := Tree_m5.Items.Count - 1do
wnto 0do
begin
AreaData := Tree_m5.Items[j].Data;
if Copy(FieldByName('Acode').AsString, 1, AreaData.Len) = AreaData.ID then
begin
tmpNode := Tree_m5.Items[j];
Break;
end;
end;
tmpNode := Tree_m5.Items.AddChild(tmpNode, FieldByName('Aname').AsString);
new(AreaData);
AreaData.ID := FieldByName('Acode').AsString;
AreaData.Len := i * 2;
tmpNode.Data := AreaData;
Next();
end;
Close();
end;
Freeandnil();
end;
end;
//构造树形结构---------------------------------
procedure Tm5_Thread.cj;
begin
//我知道不能把建树过程全部放在Execute中,但我把代码拆来拆去总拆不好,指针不能同步。
end;
麻烦你指点。