W
wind_2005
Unregistered / Unconfirmed
GUEST, unregistred user!
我写了下面一个线程类,该线程从数据库中取出数据,最终将取出的数据加载到主线程的一个combobox控件中。加载到combobox控件的Objects列表中是一个个Panel组件,该panle组件上有多个子panel,每个子panel上都有若干image控件。 在主线程中,当选择combobox控件选项时,会将该combobox控件选项中包含的panel组件及包含的所有子组件显示到窗体界面上。 存在的问题是:组件显示到窗体上之后,组件内包含的个别image组件显示不正常。 如果不用多线程,直接在主线程中以相同的方式加载数据后,完全正常。 还望各们大侠帮帮忙,谢谢了! TLoadComponentThread = class(TThread) private ComponentList: TComboBox
InComponentList: TStringList
ADOQuery: TADOQuery
protected procedure Execute
override
destructor Destroy
override
procedure GetComponent
procedure SetToMainList
//将所获取到的对象列表加载至主线程的对象列表中 public constructor Create(aComponentList: TComboBox
aConnection: TADOConnection)
end
{ TLoadComponentThread } constructor TLoadComponentThread.Create(aComponentList: TComboBox
aConnection: TADOConnection)
begin InComponentList := TStringList.Create
ADOQuery := TADOQuery.Create(nil)
ADOQuery.Connection := aConnection
ComponentList := aComponentList
FreeOnTerminate := True
inherited Create(False)
end
destructor TLoadComponentThread.Destroy
var i: Integer
begin FreeAndNil(InComponentList)
FreeAndNil(ADOQuery)
inherited
end
procedure TLoadComponentThread.Execute
begin inherited
GetComponent
end
//在此过程中将数据库中取出的数据加载到InComponentList procedure TLoadComponentThread.GetComponent
var i: Integer
BlobStream: TADOBlobStream
aHouse: TComponentHouse
begin //获取数据 ADOQuery.SQL.Text := 'SELECT * FROM S_Component'
if ADOQuery.Active then ADOQuery.Close
ADOQuery.Open
//将数据库中取出的数据加载到InComponentList for i := 0 to ADOQuery.RecordCount - 1 do begin if Terminated then Exit
//此处的'COMPONENT'字段保存了一个Panel组件,该Panel组件上有若干Panel或Image组件 BlobStream := TADOBlobStream.Create(TBlobField(ADOQuery.FieldByName('COMPONENT')), bmRead)
try aHouse := BlobStream.ReadComponent(aHouse) as TComponentHouse
aHouse.BindEven
InComponentList.AddObject(aHouse.ComponentHouseName, aHouse)
aHouse := nil
finally BlobStream.Free
end
ADOQuery.Next
end
Synchronize(SetToMainList)
end
//将InComponentList中的数据转移到主线程的ComponentList procedure TLoadComponentThread.SetToMainList
var i: Integer
begin for i := 0 to InComponentList.Count - 1 do begin if Terminated then Exit
ComponentList.Items.AddObject(InComponentList.Strings, (InComponentList.Objects as TComponentHouse))
FToMainIndex := i
end
end;
InComponentList: TStringList
ADOQuery: TADOQuery
protected procedure Execute
override
destructor Destroy
override
procedure GetComponent
procedure SetToMainList
//将所获取到的对象列表加载至主线程的对象列表中 public constructor Create(aComponentList: TComboBox
aConnection: TADOConnection)
end
{ TLoadComponentThread } constructor TLoadComponentThread.Create(aComponentList: TComboBox
aConnection: TADOConnection)
begin InComponentList := TStringList.Create
ADOQuery := TADOQuery.Create(nil)
ADOQuery.Connection := aConnection
ComponentList := aComponentList
FreeOnTerminate := True
inherited Create(False)
end
destructor TLoadComponentThread.Destroy
var i: Integer
begin FreeAndNil(InComponentList)
FreeAndNil(ADOQuery)
inherited
end
procedure TLoadComponentThread.Execute
begin inherited
GetComponent
end
//在此过程中将数据库中取出的数据加载到InComponentList procedure TLoadComponentThread.GetComponent
var i: Integer
BlobStream: TADOBlobStream
aHouse: TComponentHouse
begin //获取数据 ADOQuery.SQL.Text := 'SELECT * FROM S_Component'
if ADOQuery.Active then ADOQuery.Close
ADOQuery.Open
//将数据库中取出的数据加载到InComponentList for i := 0 to ADOQuery.RecordCount - 1 do begin if Terminated then Exit
//此处的'COMPONENT'字段保存了一个Panel组件,该Panel组件上有若干Panel或Image组件 BlobStream := TADOBlobStream.Create(TBlobField(ADOQuery.FieldByName('COMPONENT')), bmRead)
try aHouse := BlobStream.ReadComponent(aHouse) as TComponentHouse
aHouse.BindEven
InComponentList.AddObject(aHouse.ComponentHouseName, aHouse)
aHouse := nil
finally BlobStream.Free
end
ADOQuery.Next
end
Synchronize(SetToMainList)
end
//将InComponentList中的数据转移到主线程的ComponentList procedure TLoadComponentThread.SetToMainList
var i: Integer
begin for i := 0 to InComponentList.Count - 1 do begin if Terminated then Exit
ComponentList.Items.AddObject(InComponentList.Strings, (InComponentList.Objects as TComponentHouse))
FToMainIndex := i
end
end;