哎,我又不得不再一次贴出这个东东, 虽然菜了点,但希望对你有点帮助! unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls;type TForm1 = class(TForm) Edit1: TEdit; Image1: TImage; Button1: TButton; procedure Button1Click(Sender: TObject); private { Private declarations } procedure ThreadDone(Sender: TObject); public { Public declarations } end;TMyThread = class(TThread) private child : TComponent; step : integer; FormThread : TForm1; public procedure draw; constructor Create(parent : TComponent); destructor destroy; procedure Execute; override; end;var Form1: TForm1; implementation{$R *.dfm}{ TMyThread }constructor TMyThread.Create(parent: TComponent); begin child := parent; FreeOnTerminate := True; inherited Create(false); end;destructor TMyThread.destroy; begin if FormThread <> nil then FreeAndNil(FormThread); inherited; end;procedure TMyThread.draw; begin if (child is TEdit) then begin (child as TEdit).Text := inttostr(step); end else if(child is TImage) then begin (child as TImage).Canvas.Brush.Color := TColor(step*4); (child as TImage).Canvas.FillRect(rect(0,0,100,100)); end; end;procedure TMyThread.Execute; var i : integer; begin inherited; for i:= 0 to 10000 do begin step := i; if step = 5000 then begin //SendMessage(Form1.Handle,WM_SYSCOMMAND,SC_MINIMIZE,0); exit; end; synchronize(draw); end;end; procedure TForm1.Button1Click(Sender: TObject); begin with TMythread.Create(Edit1) do OnTerminate := ThreadDone; //TMythread.Create(image1);end;procedure TForm1.ThreadDone(Sender :TObject); begin TMythread.Create(image1); end;end.
虽然菜了点,但希望对你有点帮助!
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;type
TForm1 = class(TForm)
Edit1: TEdit;
Image1: TImage;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
procedure ThreadDone(Sender: TObject);
public
{ Public declarations }
end;TMyThread = class(TThread)
private
child : TComponent;
step : integer;
FormThread : TForm1; public
procedure draw;
constructor Create(parent : TComponent);
destructor destroy;
procedure Execute; override; end;var
Form1: TForm1;
implementation{$R *.dfm}{ TMyThread }constructor TMyThread.Create(parent: TComponent);
begin
child := parent;
FreeOnTerminate := True;
inherited Create(false);
end;destructor TMyThread.destroy;
begin
if FormThread <> nil then
FreeAndNil(FormThread);
inherited;
end;procedure TMyThread.draw;
begin
if (child is TEdit) then
begin
(child as TEdit).Text := inttostr(step);
end
else if(child is TImage) then
begin
(child as TImage).Canvas.Brush.Color := TColor(step*4);
(child as TImage).Canvas.FillRect(rect(0,0,100,100));
end;
end;procedure TMyThread.Execute;
var
i : integer;
begin
inherited;
for i:= 0 to 10000 do
begin
step := i;
if step = 5000 then
begin
//SendMessage(Form1.Handle,WM_SYSCOMMAND,SC_MINIMIZE,0);
exit;
end;
synchronize(draw);
end;end;
procedure TForm1.Button1Click(Sender: TObject);
begin
with TMythread.Create(Edit1) do
OnTerminate := ThreadDone;
//TMythread.Create(image1);end;procedure TForm1.ThreadDone(Sender :TObject);
begin
TMythread.Create(image1);
end;end.
一直这样循环,直到得到的参数为空为止,该如何做呢?
有的竟然只为了一个简单的赋值!
至于你这个,我也有点不明白,你究竟想做什么,能说得更清楚一些吗?
按我现在的理解,你根本没有必要先结束,又执行的,不都是一个线程吗?
你的参数难道不是在线程中改变的吗?
如果你真想那样做的话,也可以在
//SendMessage()这个地方开始线程,也就是在第一个线程退出前!
什么时候简直就是费力不讨好的事!
我以为:
1.当一个行为耗时较长时,应该用多线程,这样一来可以让用户Cancel掉这个线程!
2.大量的数学运算;
3.一个行为,他是否结束不太影响用户的后续行为,比如说向某个服务器传输文件,若用户
只关心起结果,最好用线程,所须做的只是当线程退出时告诉用户操作结果,并接受用户的处理!以上是我的浅薄之见,还望高人指点一二!
其实我想做的就象FlashGet和网络蚂蚁一样的多线程下载工具,谢谢你的答复, 如果你有好的方法,还望你能够帮我一吧。
我要做的就是离线浏览器,你知道哪里有源码吗?