线程创建之后......(100分)

  • 主题发起人 主题发起人 shopman
  • 开始时间 开始时间
S

shopman

Unregistered / Unconfirmed
GUEST, unregistred user!
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
Tmylabel=class(TThread)
private
FFlabel: Tlabel;
ii:integer;
procedure labelcount;
protected
procedure Execute;
override;
public
constructor Create(Flabel:Tlabel);
end;

type
TForm1 = class(TForm)
Label1: TLabel;
Label2: TLabel;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
implementation
{$R *.DFM}
procedure Tmylabel.Execute ;
var
i:integer;
begin
for i:=0 to 5000do
begin
ii:=i;
Synchronize(labelcount);
end;
end;

constructor Tmylabel.Create(Flabel:Tlabel);
begin
FFlabel:=Flabel;
FreeOnTerminate := True;
inherited Create(False);
end;

procedure Tmylabel.labelcount ;
begin
FFlabel.Caption :=inttostr(ii);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
Tmylabel.Create(form1.label1);
Tmylabel.Create(form1.label2);
end;

end.


它运行之后我想点击任何一个label它能停下。
请指点!!!
 

Var
TL1 :TMyLabel ;
//定义线程变量
TL2 :TMyLabel ;

{$R *.DFM}
procedure Tmylabel.Execute ;
var
i:integer;
begin
for i:=0 to 5000do
begin
if Terminated then
exit ;
//判别一下
ii:=i;
Synchronize(labelcount);
end;
end;

constructor Tmylabel.Create(Flabel:Tlabel);
begin
FFlabel:=Flabel;
FreeOnTerminate := True;
inherited Create(False);
end;

procedure Tmylabel.labelcount ;
begin
FFlabel.Caption :=inttostr(ii);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
TL1:=Tmylabel.Create(label1);
TL2:=Tmylabel.Create(label2);
end;

//下为Label1和Label2的OnClick事件
procedure TForm1.Label1Click(Sender: TObject);

begin
if (Sender as Tlabel) = Label1 then
begin
Tl1.Terminate;
end else
if (Sender as Tlabel) = Label2 then
Tl2.Terminate ;
end;

 
我有现成的源程序,要不??
 
呵呵, www是不是有好多源程序呀? 嘻嘻. 有关多线程的能否给俺两个?
town@263.net :)
 
线程的实例能否给本人?
 
有人想要有关线程的源程序请给我来信shangrw@163.net
 
多线程源程序也给小弟几个。
谢谢!
 
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;
type
Tmylabel=class(TThread)
private
FFlabel: Tlabel;
FOldClick:TNotifyEvent;
ii:integer;
procedure labelcount;
procedure NewOnClick(Sender:TObject);
protected
procedure Execute;
override;
public
constructor Create(Flabel:Tlabel);
destructor Destroy;override;
end;

type
TForm1 = class(TForm)
Label1: TLabel;
Label2: TLabel;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
implementation
{$R *.DFM}
procedure Tmylabel.Execute ;
begin
while not Terminateddo
begin
Inc(ii);
Synchronize(labelcount);
end;
end;

procedure Tmylabel.NewOnClick(Sender:TObject);
begin
if Assigned(FOldClick) then
FOldClick(Sender);
Terminated:=True;
end;

constructor Tmylabel.Create(Flabel:Tlabel);
begin
FFlabel:=FLabel;
FreeOnTerminate := True;
ii:=0;
FOldClick:=FLabel;
FLabel.OnClick:=NewOnClick;
inherited Create(False);
end;

destructor TmyLabel.Destroy;
begin
FLabel.OnClick:=FOldClick;
inherited Destroy;
end;

procedure Tmylabel.labelcount ;
begin
FFlabel.Caption :=inttostr(ii);
end;


procedure TForm1.Button1Click(Sender: TObject);
begin
Tmylabel.Create(form1.label1);
Tmylabel.Create(form1.label2);
end;

end.
 
var
L1,L2:TMyLabel;
procedure TFrom1.Button1Click(Sender:TObject);
begin
L1:=TMyLabel.Create(Form1.Label1);
L2:=TMyLabel.Create(Form2.Label2);
end;

procedure TForm1.FromClose(Sender);
begin
L1.Free;
L2.Free;
end;
 
多人接受答案了。
 
后退
顶部