uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Unit2,ExtCtrls, Menus;
type
TForm1 = class(TForm)
GroupBox1: TGroupBox;
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
Panel1: TPanel;
Label1: TLabel;
Panel2: TPanel;
Label2: TLabel;
MainMenu1: TMainMenu;
File1: TMenuItem;
New1: TMenuItem;
Open1: TMenuItem;
Save1: TMenuItem;
Saveas1: TMenuItem;
N1: TMenuItem;
Exit1: TMenuItem;
Edit1: TMenuItem;
Cut1: TMenuItem;
GroupBox2: TGroupBox;
Button6: TButton;
Button7: TButton;
Button5: TButton;
Panel3: TPanel;
Label3: TLabel;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button5Click(Sender: TObject);
procedure Exit1Click(Sender: TObject);
procedure Button6Click(Sender: TObject);
procedure Button7Click(Sender: TObject);
private
{ Private declarations }
procedure Thread1Done(sende:Tobject);
procedure Thread2Done(sende:Tobject);
public
{ Public declarations }
Flabel2,Fabel2:Tlabel;
Thread1,Thread2:TMyThread;
function MyThreadFunc(P
ointer):Longint;stdcall;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
Button1.Enabled :=false;
Button2.Enabled :=true;
Thread1:=TMyThread.Create (form1.Label1,10000);
Thread1.OnTerminate:=thread1Done;
end;
procedure TForm1.Thread1Done(sende:Tobject);
begin
Button1.Enabled :=true;
Button2.Enabled :=false;
end;
procedure TForm1.Thread2Done(sende:Tobject);
begin
Button3.Enabled :=true;
Button4.Enabled :=false;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
Thread1.Terminate;
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
Button3.Enabled :=false;
Button4.Enabled :=true;
Thread2:=TMyThread.Create (form1.Label2,10000);
Thread2.OnTerminate:=thread2Done;
end;
procedure TForm1.Button4Click(Sender: TObject);
begin
Thread2.Terminate ;
end;
procedure TForm1.Button5Click(Sender: TObject);
begin
close;
end;
procedure TForm1.Exit1Click(Sender: TObject);
begin
close;
end;
// ××××××××××××××实例二 ××××××××××××
//这是线程函数,它可以放在下面程序的任何地方
function TForm1.MyThreadFunc(P
ointer):Longint;stdcall;
var
i:integer;
DC:HDC;
S:string;
begin
DC:=GetDC(Panel3.Handle);
for i:=0 to 100000do
begin
S:=Inttostr(i);
Textout(DC,10,10,Pchar(S),length(S));
end;
ReleaseDC(Panel3.Handle,DC);
end;
procedure TForm1.Button6Click(Sender: TObject);
var
i:integer;
DC:HDC;
S:string;
begin
DC:=GetDC(Panel3.Handle);
for i:=0 to 100000do
begin
S:=Inttostr(i);
Textout(DC,10,10,Pchar(S),length(S));
end;
ReleaseDC(Panel3.Handle,DC);
end;
procedure TForm1.Button7Click(Sender: TObject);
begin
MyThreadfunc(nil);
//没有创建线程时,直接调用线程函数
end;
end.
unit Unit2;
interface
uses
Classes,stdctrls,SysUtils;
type
TMyThread = class(TThread)
private
{ Private declarations }
Flabel:Tlabel;
{传递当前操作的labeld名字}
Max:integer;
{传递要显示的最大数}
Fidx:integer;
{当前要显示的数}
Procedure updatelabel;
protected
procedure Execute;
override;
public
constructor create(Mylabel:Tlabel;number:integer);
{线程构造函数}
end;
implementation
uses unit1;
{ Important: Methods and properties of objects in VCL or CLX can only be used
in a method called using Synchronize, for example,
Synchronize(UpdateCaption);
and UpdateCaption could look like,
procedure TMyThread.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end;
}
{ TMyThread }
constructor TMyThread.create(Mylabel:Tlabel;number:integer);
{线程构造函数}
begin
inherited Create(false);
if number>1000000
then
number:=1000000;
if number<1
then
number:=1;
Max:=number;
Flabel:=mylabel;
FreeOnTerminate:=true;
end;
procedure TMyThread.Execute;
var idx:Integer;
begin
{ Place thread code here }
for idx:=0 to maxdo
begin
synchronize (updatelabel);
{注意此处}
fidx:=idx;
if terminated
then
exit;
end;
end;
Procedure TMyThread.updatelabel;
begin
flabel.caption:=inttostr(Fidx);
end;
end.