给你个例子吧:
unit copythread;
interface
uses
Classes, SysUtils;
Const
KB1 = 1024;
MB1 = 1024*KB1;
GB1 = 1024*MB1;
type
TCopyFile = class(TThread)
public
Percent : Integer;
do
ne,ToDo : Integer;
Start : TDateTime;
constructor Create(Src, Dest: String);
private
{ Private declarations }
IName,OName : String;
protected
procedure Execute;
override;
end;
implementation
{ TCopyFile }
constructor TCopyFile.Create(Src, Dest : String);
begin
IName := Src;
OName := Dest;
Percent := 0;
Start := Now;
FreeOnTerminate := True;
inherited Create(True);
end;
procedure TCopyFile.Execute;
var
fi,fo : TFileStream;
do
d,did : Integer;
cnt,max : Integer;
begin
Start := Now;
try
{ Open existing destination }
fo := TFileStream.Create(OName, fmOpenReadWrite);
fo.Position:=fo.size;
except
{ otherwise Create destination }
fo := TFileStream.Create(OName, fmCreate);
end;
try
{ open source }
fi := TFileStream.Create(IName, fmOpenRead);
try
{ synchronise dest en src }
cnt:= fo.Position;
fi.Position := cnt;
max := fi.Size;
ToDo := Max-cnt;
do
ne := 0;
{ start copying }
Repeat
do
d := MB1;
// Block size
if cnt+dod>max then
do
d := max-cnt;
ifdo
d>0 then
did := fo.CopyFrom(fi,do
d);
cnt:=cnt+did;
Percent := Round(Cnt/Max*100);
do
ne :=do
ne+did;
ToDo := Max;
until (dod=0) or (Terminated);
finally
fi.free;
end;
finally
fo.free;
end;
end;