帮忙写个多线程程序.(50分)

  • 主题发起人 主题发起人 hhjjhhjj
  • 开始时间 开始时间
H

hhjjhhjj

Unregistered / Unconfirmed
GUEST, unregistred user!
怎样把这个过程写成多线程的?
主窗体上有个TreeView,
过程作用是把数据库中的数据生成一个树,显示在Treeview
当记录多时就很慢,
其中又用到一个函数。

Procedure Opentree(Tv: TTreeView;
AQ:TADOQuery;Ds:Tdataset );
begin
With AQdo
begin
Close;
SQL.Clear;
SQL.Text := 'SELECT * FROM Note_tree ORDER BY tlevel ASC ,torder ASC';
Open;
TV.Items.Clear;
If Not IsEmpty then
begin
CreateProducetree(tv,ds,'tid','tname','tlevel','tfather');
Close;
end;
end;
end;

Function CreateProducetree(Tv:TTreeview;Ds:Tdataset;Id,Name,Level,Father
: String):Boolean;
....
end;
 
procedure myxc.Execute;
var
Tv: TTreeView;
AQ:TADOQuery;Ds:Tdataset;
begin
{ Place thread code here }
With AQdo
begin
Close;
SQL.Clear;
SQL.Text := 'SELECT * FROM Note_tree ORDER BY tlevel ASC ,torder ASC';
Open;
TV.Items.Clear;
If Not IsEmpty then
begin
CreateProducetree(tv,ds,'tid','tname','tlevel','tfather');
Close;
end;
end;

end;
Function CreateProducetree(Tv:TTreeview;Ds:Tdataset;Id,Name,Level,Father
: String):Boolean;
....
end;
 
你可以参考demo里的 chat例程,它里有socket和treeview的同步,就是生成一个线程来做的
//------------- TClientDataThread impl -----------------------------------------
constructor TClientDataThread.Create(CreateSuspended: Boolean);
begin
inherited Create(CreateSuspended);
FreeOnTerminate := true;
ListBuffer := TStringList.Create;
end;

procedure TClientDataThread.Terminate;
begin
ListBuffer.Free;
inherited;
end;

procedure TClientDataThread.Execute;
begin
Synchronize(synchAddDataToControl);
end;

procedure TClientDataThread.synchAddDataToControl;
begin
TargetList.AddStrings(ListBuffer);
end;
//------------- end TClientDataThread impl -------------------------------------

procedure TForm1.btnActivateServerClick(Sender: TObject);
begin
TcpServer1.LocalPort := edtLocalPort.Text;
TcpServer1.Active := True;
end;

procedure TForm1.btnSendClick(Sender: TObject);
var
I: Integer;
begin
TcpClient1.RemoteHost := edtRemoteHost.Text;
TcpClient1.RemotePort := edtRemotePort.Text;
try
if TcpClient1.Connect then
for I := 0 to memSend.
Lines.Count - 1do
TcpClient1.Sendln(memSend.
Lines);
finally
TcpClient1.Disconnect;
end;
end;

procedure TForm1.TcpServer1Accept(sender: TObject;
ClientSocket: TCustomIpClient);
var
s: string;
DataThread: TClientDataThread;
begin
// create thread
DataThread:= TClientDataThread.Create(true);
// set the TagetList to the gui list that you
// with to synch with.
DataThread.TargetList := memRecv.lines;
// Load the Threads ListBuffer
DataThread.ListBuffer.Add('*** Connection Accepted ***');
DataThread.ListBuffer.Add('Remote Host: ' + ClientSocket.LookupHostName(ClientSocket.RemoteHost) +
' (' + ClientSocket.RemoteHost + ')');
DataThread.ListBuffer.Add('===== begin
message =====');
s := ClientSocket.Receiveln;
while s <> ''do
begin
DataThread.ListBuffer.Add(s);
s := ClientSocket.Receiveln;
end;
DataThread.ListBuffer.Add('===== End of message =====');
// Call Resume which will execute and synch the
// ListBuffer with the TargetList
DataThread.Resume;
end;
 
多人接受答案了。
 
后退
顶部