Author: Erwin Molendijk Demo of file coping (TFileStream) in a thread (TThread). It also resumes copying of files that have been partialy copied. Answer: 1 unit copythread; 2 3 interface 4 5 uses 6 Classes, SysUtils; 7 8 const 9 KB1 = 1024; 10 MB1 = 1024 * KB1; 11 GB1 = 1024 * MB1; 12 13 type 14 TCopyFile = class(TThread) 15 public 16 Percent: Integer; 17 Done, ToDo: Integer; 18 Start: TDateTime; 19 constructor Create(Src, Dest: string); 20 private 21 { Private declarations } 22 IName, OName: string; 23 protected 24 procedure Execute; override; 25 end; 26 27 implementation 28 29 { TCopyFile } 30 31 constructor TCopyFile.Create(Src, Dest: string); 32 begin 33 IName := Src; 34 OName := Dest; 35 Percent := 0; 36 Start := Now; 37 FreeOnTerminate := True; 38 inherited Create(True); 39 end; 40 41 procedure TCopyFile.Execute; 42 var 43 fi, fo: TFileStream; 44 dod, did: Integer; 45 cnt, max: Integer; 46 47 begin 48 49 Start := Now; 50 try 51 { Open existing destination } 52 fo := TFileStream.Create(OName, fmOpenReadWrite); 53 fo.Position := fo.size; 54 except 55 { otherwise Create destination } 56 fo := TFileStream.Create(OName, fmCreate); 57 end; 58 try 59 { open source } 60 fi := TFileStream.Create(IName, fmOpenRead); 61 try 62 { synchronise dest en src } 63 cnt := fo.Position; 64 fi.Position := cnt; 65 max := fi.Size; 66 ToDo := Max - cnt; 67 Done := 0; 68 69 { start copying } 70 repeat 71 dod := MB1; // Block size 72 if cnt + dod > max then 73 dod := max - cnt; 74 if dod > 0 then 75 did := fo.CopyFrom(fi, dod); 76 cnt := cnt + did; 77 Percent := Round(Cnt / Max * 100); 78 79 Done := Done + did; 80 ToDo := Max; 81 until (dod = 0) or (Terminated); 82 83 finally 84 fi.free; 85 end; 86 finally 87 fo.free; 88 end; 89 end; 90 91 end.