谁有多线程复制文件的源码吗?(20分)

  • 主题发起人 主题发起人 jog81
  • 开始时间 开始时间
J

jog81

Unregistered / Unconfirmed
GUEST, unregistred user!
如题,请各位高手帮忙!谢谢!
 
老大,没人知道呀?还是不屑回答?
 
我有我给你;
这是我的一段代码,功能是del,copy文件(在线程中,处理完了向窗口发送消息)
unit UThreadCopyFile;
interface
uses
Windows,Classes,SysUtils,Messages;
Const
WM_COPYOK = WM_USER+1000;
WM_DELETEOK = WM_USER+1010;
fm_copy = 1;
fm_delete = 2;
type
TCopyFileThread = class(TThread)
private
{ Private declarations }
Spath,Dpath,SFileName:String;
iType:Integer;
CallBackWindow:THandle;
procedure DelFileAll(AFileName:String);
procedure CopyFileAll(AFileName,ADpath:String);
protected
procedure Execute;
override;
public
constructor Create(ASFileName,ADpath:String;
ACallBackWindow:THandle;AiType:integer;IsRun: boolean);
end;

implementation
{ Important: Methods and properties of objects in visual components can only be
used in a method called using Synchronize, for example,
Synchronize(UpdateCaption);
and UpdateCaption could look like,
procedure TCopyFileThread.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end;
}
{ TCopyFileThread }
constructor TCopyFileThread.Create(ASFileName,ADpath:String;
ACallBackWindow:THandle;AiType:integer;IsRun: boolean);
begin
Spath:=ExtractFilePath(ASFileName);
SFileName:=ASFileName;
Dpath:=ADpath;
CallBackWindow:=ACallBackWindow;
iType:=AiType;
inherited Create(IsRun);
end;

procedure TCopyFileThread.Execute;
begin
{ Place thread code here }
if iType=fm_copy then
begin
CopyFileAll(SFileName,Dpath);
PostMessage(CallBackWindow,WM_COPYOK,0,0);
end
else
if iType=fm_delete then
begin
DelFileAll(SFileName);
PostMessage(CallBackWindow,WM_DELETEOK,0,0);
end;
end;

procedure TCopyFileThread.CopyFileAll(AFileName,ADpath:String);
Var
sr: TSearchRec;
sFileName:string;
// sNowTime,sFileTime:String;
begin
// sNowTime:=FormatDateTime('YYYY-MM-DD',Now);
if FindFirst(AFileName,faAnyFile,sr)=0 then
begin
sFileName := sr.Name;
if (sr.Attr AND faDirectory)=0 then
CopyFile(Pchar(ExtractFilePath(AFileName)+sFileName),Pchar(ADpath+sFileName),False);
end;
while FindNext(sr) = 0do
begin
sFileName := sr.Name;
if (sr.Attr AND faDirectory)=0 then
CopyFile(Pchar(ExtractFilePath(AFileName)+sFileName),Pchar(ADpath+sFileName),False);
end;
end;

procedure TCopyFileThread.DelFileAll(AFileName:String);
Var
sr: TSearchRec;
sFileName:string;
begin
if FindFirst(AFileName,faAnyFile,sr)=0 then
begin
sFileName := ExtractFilePath(AFileName)+sr.Name;
if (sr.Attr AND faDirectory)=0 then
DeleteFile(sFileName);
end;
while FindNext(sr) = 0do
begin
sFileName := ExtractFilePath(AFileName)+sr.Name;
if (sr.Attr AND faDirectory)=0 then
DeleteFile(sFileName);
end;
end;

end.
 
怎么看这段代码都很搞笑。
你对复制文件有什么特别的要求吗?为什么一定要到线程中处理?
 
我主要是想使复制文件的速度尽量加快,因为我经常需要复制文件!
 
使用线程并不能加快你复制文件的速度。
 
同意,多线程并不能加快速度,因为时间片的分配都占用了,多线程还在在同一个时间片,但是可以稍微提高线程的线别进行加快一点速度
 
同意,多线程并不能加快速度!!![:)]
 
呵,谢谢大家。
 
后退
顶部