--- In [email protected], "rodveilleux" <[EMAIL PROTECTED]> wrote:
>
> Hello,
> Trying to display a progress bar in Delphi 2006 while copying a 
> large file. My example consists of a VCL Form, 
> Button,OpenDialog,Gauge and Timer.
> My problem is: The bar will not progress while Copy function is 
> executing.
> 
> Thanks,
> Rodney
>
 
The problem is that CopyFile is being run in the main thread.
This means it's already too busy to respond to OnTimer events.
Instead, you should execute CopyFile in a thread.
That is fairly simple to. Complete working code is below.

type
~ TCopyFileThread = class(TThread)
~ protected
~~~ procedure Execute; override;
~ public
~~~ FromFile: string;
~~~ ToFile: string;
~ end;

procedure TCopyFileThread.Execute;
begin
~ CopyFile(PChar(FromFile),PChar(ToFile),False);
end;

That's the thread done! Easy huh?

Now to call it up from your main form...

procedure TForm1.Button1Click(Sender: TObject);
var
~ thrdCopyFile: TCopyFileThread;
~ Path : string;
begin
~ if OpenDialog1.Execute then
~ begin {Create back up copy of DBF file}
~~~ Path:= ExtractFilePath(OpenDialog1.FileName);
~~~ thrdCopyFile := TCopyFileThread.Create(True);
~~~ thrdCopyFile.FreeOnTerminate := True;
~~~ thrdCopyFile.FromFile:= OpenDialog1.FileName;
~~~ thrdCopyFile.ToFile:= Path +
~~~~~ 'Copy of '+ ExtractFileName(thrdCopyFile.FromFile);
~~~ thrdCopyFile.OnTerminate := CopyDone; // see below
~~~ Gauge1.MinValue:= 0;
~~~ Gauge1.MaxValue:= 10;
~~~ Gauge1.Progress:= Gauge1.MinValue;
~~~ Timer1.Enabled:= true;
~~~ { Start the copy thread }
~~~ thrdCopyFile.Resume;
~ end;
end;

procedure TForm1.CopyDone(Sender: TObject);
begin
~ Timer1.Enabled:= False;
~ ShowMessage('Complete');
end;


Reply via email to