1.主窗体代码 unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, ComCtrls,Unit2, StdCtrls;type TForm1 = class(TForm) ProgressBar1: TProgressBar; ProgressBar2: TProgressBar; Button1: TButton; Button2: TButton; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); private { Private declarations } pb1thread:TPbProgress; pb2thread:TPbProgress; public { Public declarations } end;var Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject); begin pb1thread:=TPbProgress.Create(true);//挂起 pb1thread.FreeOnTerminate:=true; pb1thread.PbID:=1; pb1thread.Resume; end;procedure TForm1.Button2Click(Sender: TObject); begin pb2thread:=TPbProgress.Create(true);//挂起 pb2thread.FreeOnTerminate:=true; pb2thread.PbID:=2; pb2thread.Resume; end;end.2.线程单元代码unit Unit2;interfaceuses Classes;type TPbProgress = class(TThread) private { Private declarations } protected procedure Execute; override; procedure fun; public PbID:1..2; end;implementation uses Unit1;{ Important: Methods and properties of objects in visual components can only be used in a method called using Synchronize, for example, Synchronize(UpdateCaption); and UpdateCaption could look like, procedure TPbProgress.UpdateCaption; begin Form1.Caption := 'Updated in a thread'; end; }{ TPbProgress }procedure TPbProgress.Execute; var i:integer; begin { Place thread code here } i:=1; while ( i < 10000 ) do begin synchronize(fun); Inc(i); end;end;procedure TPbProgress.fun; begin if PbID=1 then form1.ProgressBar1.StepIt else form1.ProgressBar2.StepIt; end;end.
加油! demo好像有点难,看不懂! 大家加油丁啊,谢谢!!!
单元2 {利用Tedit句柄和Form1沟通}unit Unit2;interfaceuses Windows, Messages, SysUtils,Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;type mymath1 = class(TThread) private { Private declarations } protected procedure Execute; override; public edit4:Tedit; //设置一个TEdit句柄 constructor create; end;implementationconstructor mymath1.create; begin inherited create(true); end;procedure mymath1.Execute; var i:integer; begin for i:=1 to 10000 do edit4.text:=inttostr(i) end;end.
单元3 {直接调用Form1控件对象} unit Unit3;interfaceuses Windows, Messages, SysUtils,Classes, Graphics, Controls, Forms, Dialogs, StdCtrls,unit1;type mymath2 = class(TThread) private { Private declarations } protected procedure Execute; override; public constructor create; end;implementation{ mymach2 }constructor mymath2.create; begin inherited create(true); end; procedure mymath2.Execute; var i:integer; begin for i:=1 to 20000 do form1.edit2.text:=inttostr(i) end;end.
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls,Unit2, StdCtrls;type
TForm1 = class(TForm)
ProgressBar1: TProgressBar;
ProgressBar2: TProgressBar;
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
pb1thread:TPbProgress;
pb2thread:TPbProgress;
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
begin
pb1thread:=TPbProgress.Create(true);//挂起
pb1thread.FreeOnTerminate:=true;
pb1thread.PbID:=1;
pb1thread.Resume;
end;procedure TForm1.Button2Click(Sender: TObject);
begin
pb2thread:=TPbProgress.Create(true);//挂起
pb2thread.FreeOnTerminate:=true;
pb2thread.PbID:=2;
pb2thread.Resume;
end;end.2.线程单元代码unit Unit2;interfaceuses
Classes;type
TPbProgress = class(TThread)
private
{ Private declarations }
protected
procedure Execute; override;
procedure fun;
public
PbID:1..2;
end;implementation
uses Unit1;{ Important: Methods and properties of objects in visual components can only be
used in a method called using Synchronize, for example, Synchronize(UpdateCaption); and UpdateCaption could look like, procedure TPbProgress.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end; }{ TPbProgress }procedure TPbProgress.Execute;
var
i:integer;
begin
{ Place thread code here }
i:=1;
while ( i < 10000 ) do
begin
synchronize(fun);
Inc(i);
end;end;procedure TPbProgress.fun;
begin
if PbID=1 then
form1.ProgressBar1.StepIt
else
form1.ProgressBar2.StepIt;
end;end.
demo好像有点难,看不懂!
大家加油丁啊,谢谢!!!
{利用Tedit句柄和Form1沟通}unit Unit2;interfaceuses
Windows, Messages, SysUtils,Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;type
mymath1 = class(TThread) private
{ Private declarations }
protected
procedure Execute; override;
public
edit4:Tedit; //设置一个TEdit句柄
constructor create; end;implementationconstructor mymath1.create;
begin
inherited create(true);
end;procedure mymath1.Execute;
var i:integer;
begin for i:=1 to 10000 do
edit4.text:=inttostr(i)
end;end.
{直接调用Form1控件对象}
unit Unit3;interfaceuses
Windows, Messages, SysUtils,Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls,unit1;type
mymath2 = class(TThread)
private
{ Private declarations }
protected
procedure Execute; override;
public
constructor create;
end;implementation{ mymach2 }constructor mymath2.create;
begin
inherited create(true);
end;
procedure mymath2.Execute;
var i:integer;
begin
for i:=1 to 20000 do
form1.edit2.text:=inttostr(i)
end;end.
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;type
TForm1 = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.DFM}uses unit2,unit3;var thread1:mymath1;
thread2:mymath2;procedure TForm1.Button1Click(Sender: TObject);
begin
thread1.resume;
thread2.resume;
end;procedure TForm1.FormCreate(Sender: TObject);
begin
thread1:=mymath1.create;
thread2:=mymath2.create;
thread1.edit4:=edit1;
end;procedure TForm1.Button2Click(Sender: TObject);
begin
thread1.suspend;
thread2.suspend;
end;procedure TForm1.Button3Click(Sender: TObject);
begin
thread1.destroy;
thread2.destroy;
thread1:=mymath1.create;
thread2:=mymath2.create;
end;procedure TForm1.Button4Click(Sender: TObject);
begin
thread1.destroy;
thread2.destroy;
Application.Terminate;
end;end.