动态建立线程,帮帮我(50分)

  • 主题发起人 主题发起人 delphi5988
  • 开始时间 开始时间
D

delphi5988

Unregistered / Unconfirmed
GUEST, unregistred user!
兄弟们帮帮我,我想动态建立多线程,就像,网际快车一样,想动态建立,10或者100个线程,怎么处理呀,那有代码可以参考一下。谢谢大家
 
创建倒没什么,主要是你要处理好线程同步问题
 
老兄线程都是动态建的 你看一下线程的例子吧
 
在dfw上搜一下;一堆一堆的!关键词:线程 动态.....
 
Delphi5开发人员指南的例子:
unit Main;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Menus, Dialogs;
type
TMainForm = class(TForm)
MainMenu1: TMainMenu;
Options1: TMenuItem;
AddThread: TMenuItem;
RemoveThread: TMenuItem;
ColorDialog1: TColorDialog;
Add10: TMenuItem;
RemoveAll: TMenuItem;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure AddThreadClick(Sender: TObject);
procedure RemoveThreadClick(Sender: TObject);
procedure Add10Click(Sender: TObject);
procedure RemoveAllClick(Sender: TObject);
private
ThreadList: TList;
public
{ Public declarations }
end;

TDrawThread = class(TThread)
private
FColor: TColor;
FForm: TForm;
public
constructor Create(AForm: TForm;
AColor: TColor);
procedure Execute;
override;
end;

var
MainForm: TMainForm;
implementation
{$R *.DFM}
{ TDrawThread }
constructor TDrawThread.Create(AForm: TForm;
AColor: TColor);
begin
FColor := AColor;
FForm := AForm;
inherited Create(False);
end;

procedure TDrawThread.Execute;
var
P1, P2: TPoint;
procedure GetRandCoords;
var
MaxX, MaxY: Integer;
begin
// initialize P1 and P2 to random points within Form bounds
MaxX := FForm.ClientWidth;
MaxY := FForm.ClientHeight;
P1.x := Random(MaxX);
P2.x := Random(MaxX);
P1.y := Random(MaxY);
P2.y := Random(MaxY);
end;

begin
FreeOnTerminate := True;
// thread runs until it or the application is terminated
while not (Terminated or Application.Terminated)do
begin
GetRandCoords;
// initialize P1 and P2
with FForm.Canvasdo
begin
Lock;
// lock canvas
// only one thread at a time can execute the following code:
Pen.Color := FColor;
// set pen color
MoveTo(P1.X, P1.Y);
// move to canvas position P1
LineTo(P2.X, P2.Y);
// draw a line to position P2
// after the next line executes, another thread will be allowed
// to enter the above code block
Unlock;
// unlock canvas
end;
end;
end;

{ TMainForm }
procedure TMainForm.FormCreate(Sender: TObject);
begin
ThreadList := TList.Create;
end;

procedure TMainForm.FormDestroy(Sender: TObject);
begin
RemoveAllClick(nil);
ThreadList.Free;
end;

procedure TMainForm.AddThreadClick(Sender: TObject);
begin
// add a new thread to the list... allow user to choose color
if ColorDialog1.Execute then
ThreadList.Add(TDrawThread.Create(Self, ColorDialog1.Color));
end;

procedure TMainForm.RemoveThreadClick(Sender: TObject);
begin
// terminate the last thread in the list and remove it from list
TDrawThread(ThreadList[ThreadList.Count - 1]).Terminate;
ThreadList.Delete(ThreadList.Count - 1);
end;

procedure TMainForm.Add10Click(Sender: TObject);
var
i: Integer;
begin
// create 10 threads, each with a random color
for i := 1 to 10do
ThreadList.Add(TDrawThread.Create(Self, Random(MaxInt)));
end;

procedure TMainForm.RemoveAllClick(Sender: TObject);
var
i: Integer;
begin
Cursor := crHourGlass;
try
for i := ThreadList.Count - 1do
wnto 0do
begin
TDrawThread(ThreadList).Terminate;
// terminate thread
TDrawThread(ThreadList).WaitFor;
// make sure thread terminates
end;
ThreadList.Clear;
finally
Cursor:= crDefault;
end;
end;

initialization
Randomize;
// seed random number generator
end.
 
关于内存的释放问题,也很是难呀,多线程,每建立一次,就是运行一次多线程就会,多占一部分内存,这样还不把内存占完了!我的软件是常驻内存的,所以想能不能让线程用完后就释放,但是我用了那个可以释放的函数,还是没有用,能不能帮帮我,兄弟们,我快急疯了
 
定义一个Public变量ExitProcess
在Execute中初始化为False
替换while not (Terminated or Application.Terminated)do
为while not ExitProcessdo
 
终止时令ExitProcess:=True即可
 
后退
顶部