为什么用线程做的进度条不能同步????????高手请进,在线等
下面是我的线程:
unit Unit2;
interface
uses
Classes,ComCtrls;
type
mythread = class(TThread)
private
{ Private declarations }
fProgressBar1:TProgressBar;
procedure pro_bar;
protected
procedure Execute; override;
public
constructor Create(fp: TProgressBar);
end;
implementation
{ mythread }
constructor mythread.Create(fp:TProgressBar);
begin
//赋初值
fProgressBar1:=fp;
fProgressBar1.Max:=80000;
fProgressBar1.Position:=0;
FreeOnTerminate := True;
inherited Create(False);
end;
procedure mythread.Execute;
begin
Synchronize(pro_bar);
end;
procedure mythread.pro_bar;
begin
while fProgressBar1.Position <> fProgressBar1.max do
begin
fProgressBar1.Position:=fProgressBar1.Position+1;
end;
end;
end.
下面是我的调用:
var
Form3: TForm3;
implementation
uses Unit1,unit2;
{$R *.dfm}
var thrbar:mythread;
procedure TForm3.Button1Click(Sender: TObject);
begin
thrbar:=mythread.Create(ProgressBar1);
//thrbar.WaitFor();
form4.show;
end;
运行时当单击form3上的按钮时就不能同步操作了,比如最大化form3窗体,或者拖动它
[解决办法]
进度条不需要放在线程中吧。
线程是在执行一个费时的操作时为了好的界面反映才用。最主要是将执行的代码放在线程中执行,面进度条是界面元素,只是在主线程中响应的。
[解决办法]
直接发消息给主线程的进度条
[解决办法]
procedure mythread.pro_bar;
begin
while fProgressBar1.Position <> fProgressBar1.Max do
begin
fProgressBar1.Position := fProgressBar1.Position + 1;
Application.ProcessMessages;// 加上这句
end;
end;
[解决办法]
线程里
procedure mythread.Execute;
begin
PostMessage(fProgressBar1.Handle,PBM_STEPIT,0,0)
end;