Articles   Members Online: 3
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
Demo of file copying (TFileStream) in a thread (TThread) Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
12-Sep-03
Category
Win API
Language
Delphi 5.x
Views
154
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			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.


			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC