我有我给你;
这是我的一段代码,功能是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.