用线程做的进度条,怎样调用呀???????、
如题所示:我想在程序打开一个耗时窗口时,显示进度条(用线程做的),当窗口打开后,该进度条消失!下面是用线程做的进度条原码:请大家指点:在线急等:
unit Unit2;
interface
uses
Classes,ComCtrls;
type
mythread = class(TThread)
private
{ Private declarations }
fProgressBar1:TProgressBar;
protected
procedure Execute; override;
public
constructor Create(fp: TProgressBar);
end;
implementation
{ mythread }
constructor mythread.Create(fp:TProgressBar);
begin
//赋初值
fProgressBar1:=fp;
fProgressBar1.Max:=8000;
fProgressBar1.Position:=0;
FreeOnTerminate := True;
inherited Create(False);
end;
procedure mythread.Execute;
begin
{ Place thread code here }
while fProgressBar1.Position <> fProgressBar1.max do
begin
fProgressBar1.Position:=fProgressBar1.Position+1;
end;
if fProgressBar1.Position=fProgressBar1.Max then
fProgressBar1.Position:=0;
inherited;
end;
end.
[解决办法]
你这样做有问题么?线程最好做个同步来操作进度条,不知道你有没有这个必要。主界面用个等待结束就可以了吧,var
aThread: mythread;
begin
aThread := mythread.Create(ProgressBar1);
aThread.WaitFor();
Form3.ShowModal();
[解决办法]
procedure mythread.gogogo();
begin
while fProgressBar1.Position <> fProgressBar1.max do
begin
fProgressBar1.Position:=fProgressBar1.Position+1;
end;
if fProgressBar1.Position=fProgressBar1.Max then
fProgressBar1.Position:=0;
end;
procedure mythread.Execute;
begin
{ Place thread code here }
Synchronize(gogogo);
end;
[解决办法]
destructor mythread.destroy();
begin
//
inherited destroy();
end;
constructor mythread.Create(fp:TProgressBar);
begin
//赋初值
fProgressBar1:=fp;
fProgressBar1.Max:=8000;
fProgressBar1.Position:=0;
FreeOnTerminate := false;
inherited Create(False);
end;
uses
Unit2, Unit3;
procedure TForm1.Button1Click(Sender: TObject);
var
aThread: mythread;
begin
aThread := mythread.Create(ProgressBar1);
aThread.WaitFor();
aThread.Free();
Form3.ShowModal();
end;
[解决办法]
在线程中用Synchronize,跟直接在主线程中运行一个样,没有效果的。
我的建议:
耗时的操作不要在窗口打开(但未显示)时做,放到窗口显示后再做(比如在OnCreate中用PostMessage),同时在窗体上放个TAnimate播放avi,它不会影响计算过程,也很容易控制。
另外,除非你能准确知道进度值,否则用TProgressBar没多大用。