请帮我看看这个 多线程的问题。 ( 积分: 20 )

  • 主题发起人 主题发起人 zjwyyh
  • 开始时间 开始时间
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.
 
我的要求是这样,在窗体上有两个进度条,当点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.
 
没有用到多线程, 全部代码都在主线程里执行的, 所以主线程根本没机会处理消息刷新屏幕了.
 
也许是两个子线程占用了主线程时间,修改子线程优先级 Priority,在线程循环中加入 sleep释放时间
 
可以帮我改一下上面的代码吗?
 
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;
ProgressBar2: TProgressBar;
Button3: TButton;
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
inherited create(Suspended);
fpro:=pro;
fpro.Max:=10000;
FreeOnTerminate := true;
end;

procedure Tmythread.do_pro;
//var
// i:integer;
begin
//while not Terminated do
fpro.Position:=fPro.Position+1;
end;

procedure Tmythread.Execute;
var
i:integer;
begin
//While not Terminated do
for i:=0 to 10000 do
begin
//Form1.Label1.Caption:=inttostr(i);
Synchronize(do_pro);{ 线程同步 }
end;
//do_pro;
end;

procedure TForm1.Button1Click(Sender: TObject);
//var
//t1,t2:Tmythread;
begin
t1:=Tmythread.Create(true,progressbar1);
t2:=Tmythread.Create(true,progressbar2);
t1.Resume;
t2.Resume;
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
t1.Terminate; { 销毁之前终止线程执行 }
t1.Destroy;
t2.Terminate; { 销毁之前终止线程执行 }
t2.Destroy;
end;

end.

因为循环很耗CPU,如果你发现主线程还会没响应,
你可以在Tmythread.Create中加入
Priority:=tpLower;
 
谢谢czcn老兄了。
 
后退
顶部