为什么我的线程执行起来那么困难 甚至象死机了一样啊: unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls;type
TCaptionThread = class(TThread)
private
Panelss :TPanel;
Labelss :TLabel;
MoveMethod :Boolean;
procedure CaptionMove;
protected
procedure Execute;override;
public
Constructor Create(CreateSuspend:Boolean;Panels:TPanel;Lablels:TLabel;MoveFlag:Boolean);
end; TForm1 = class(TForm)
Panel1: TPanel;
Label1: TLabel;
Label2: TLabel;
Panel2: TPanel;
Button1: TButton;
Button2: TButton;
Button3: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
CaptionThreadTop : TCaptionThread;
CaptionThreadBottom : TCaptionThread;
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}Constructor TCaptionThread.Create(CreateSuspend:Boolean;Panels:TPanel;Lablels:TLabel;MoveFlag:Boolean);
begin
FreeOnTerminate := true;
Panelss := Panels;
Labelss := Lablels;
MoveMethod := MoveFlag;
Inherited Create(CreateSuspend);
end;procedure TCaptionThread.CaptionMove;
begin
while not Terminated do
begin
if MoveMethod then
begin
if Labelss.Left = -Labelss.Width then
Labelss.Left := Panelss.Width
else Labelss.Left := Labelss.Left -1;
end
else
begin
if Labelss.Left = Panelss.Width then
Labelss.Left := 0
else Labelss.Left := Labelss.Left +1;
end; end;
end;procedure TCaptionThread.Execute;
begin
Synchronize(CaptionMove);
end;procedure TForm1.Button1Click(Sender: TObject);
begin
CaptionThreadTop.Suspend;
end;procedure TForm1.Button2Click(Sender: TObject);
begin
CaptionThreadTop.Resume;
end;procedure TForm1.Button3Click(Sender: TObject);
begin
CaptionThreadTop:=TCaptionThread.Create(false,Panel2,Label2,false);
CaptionThreadBottom:=TCaptionThread.Create(false,Panel1,Label1,true);
end;end.
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls;type
TCaptionThread = class(TThread)
private
Panelss :TPanel;
Labelss :TLabel;
MoveMethod :Boolean;
procedure CaptionMove;
protected
procedure Execute;override;
public
Constructor Create(CreateSuspend:Boolean;Panels:TPanel;Lablels:TLabel;MoveFlag:Boolean);
end; TForm1 = class(TForm)
Panel1: TPanel;
Label1: TLabel;
Label2: TLabel;
Panel2: TPanel;
Button1: TButton;
Button2: TButton;
Button3: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
CaptionThreadTop : TCaptionThread;
CaptionThreadBottom : TCaptionThread;
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}Constructor TCaptionThread.Create(CreateSuspend:Boolean;Panels:TPanel;Lablels:TLabel;MoveFlag:Boolean);
begin
FreeOnTerminate := true;
Panelss := Panels;
Labelss := Lablels;
MoveMethod := MoveFlag;
Inherited Create(CreateSuspend);
end;procedure TCaptionThread.CaptionMove;
begin
while not Terminated do
begin
if MoveMethod then
begin
if Labelss.Left = -Labelss.Width then
Labelss.Left := Panelss.Width
else Labelss.Left := Labelss.Left -1;
end
else
begin
if Labelss.Left = Panelss.Width then
Labelss.Left := 0
else Labelss.Left := Labelss.Left +1;
end; end;
end;procedure TCaptionThread.Execute;
begin
Synchronize(CaptionMove);
end;procedure TForm1.Button1Click(Sender: TObject);
begin
CaptionThreadTop.Suspend;
end;procedure TForm1.Button2Click(Sender: TObject);
begin
CaptionThreadTop.Resume;
end;procedure TForm1.Button3Click(Sender: TObject);
begin
CaptionThreadTop:=TCaptionThread.Create(false,Panel2,Label2,false);
CaptionThreadBottom:=TCaptionThread.Create(false,Panel1,Label1,true);
end;end.
实际上你的线程都在主线程中跑,有什么意义呢?
1, CaptionMove 死循环, 应该将 while not Terminated do
放在Execute 中.
2, thread 循环中应该加上Sleep,让出cpu 时间.
3, MoveMethod 在建立时付值 , 无法修改.