Z
zjwyyh
Unregistered / Unconfirmed
GUEST, unregistred user!
我的要求是这样,在窗体上有两个进度条,当点button1时生成两个线程,两个进度条同时进行。我写了如下的代码,发现当两个进度条进行时,主线程并不是空闲的(我不能按其它按钮,也不能拖动窗体),请问这是咋回事呀。谢谢!
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls, ComCtrls;
type
TForm1 = class(TForm)
Label1: TLabel;
ProgressBar1: TProgressBar;
Button1: TButton;
Button3: TButton;
ProgressBar2: TProgressBar;
procedure Button1Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
Tmythread=class(TThread)
private
//Fposition:integer;
fpro:Tprogressbar;
procedure do_pro;
protected
procedure Execute; override;{ 执行线程的方法 }
public
constructor Create(Suspended:Boolean;pro:Tprogressbar);
end;
var
Form1: TForm1;
t1,t2:Tmythread;
implementation
{$R *.dfm}
constructor Tmythread.Create(Suspended:Boolean;pro:Tprogressbar);
begin
fpro:=pro;
inherited create(Suspended);
FreeOnTerminate := true;
end;
procedure Tmythread.do_pro;
var
i:integer;
begin
fpro.Max:=10000;
for i:=0 to 10000 do
begin
// Form1.Label1.Caption:=inttostr(i);
//while not Terminated do
fpro.Position:=fPro.Position+1;
end;
end;
procedure Tmythread.Execute;
begin
//While not Terminated do
Synchronize(do_pro);{ 线程同步 }
//do_pro;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
t1,t2:Tmythread;
begin
t1:=Tmythread.Create(false,progressbar1);
t2:=Tmythread.Create(false,progressbar2);
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
t1.Terminate; { 销毁之前终止线程执行 }
t1.Destroy;
t2.Terminate; { 销毁之前终止线程执行 }
t2.Destroy;
end;
end.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls, ComCtrls;
type
TForm1 = class(TForm)
Label1: TLabel;
ProgressBar1: TProgressBar;
Button1: TButton;
Button3: TButton;
ProgressBar2: TProgressBar;
procedure Button1Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
Tmythread=class(TThread)
private
//Fposition:integer;
fpro:Tprogressbar;
procedure do_pro;
protected
procedure Execute; override;{ 执行线程的方法 }
public
constructor Create(Suspended:Boolean;pro:Tprogressbar);
end;
var
Form1: TForm1;
t1,t2:Tmythread;
implementation
{$R *.dfm}
constructor Tmythread.Create(Suspended:Boolean;pro:Tprogressbar);
begin
fpro:=pro;
inherited create(Suspended);
FreeOnTerminate := true;
end;
procedure Tmythread.do_pro;
var
i:integer;
begin
fpro.Max:=10000;
for i:=0 to 10000 do
begin
// Form1.Label1.Caption:=inttostr(i);
//while not Terminated do
fpro.Position:=fPro.Position+1;
end;
end;
procedure Tmythread.Execute;
begin
//While not Terminated do
Synchronize(do_pro);{ 线程同步 }
//do_pro;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
t1,t2:Tmythread;
begin
t1:=Tmythread.Create(false,progressbar1);
t2:=Tmythread.Create(false,progressbar2);
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
t1.Terminate; { 销毁之前终止线程执行 }
t1.Destroy;
t2.Terminate; { 销毁之前终止线程执行 }
t2.Destroy;
end;
end.