帮我看看这段代码,该如何做。有关多线程的。谢谢(50分)

  • 主题发起人 主题发起人 huangbaili
  • 开始时间 开始时间
H

huangbaili

Unregistered / Unconfirmed
GUEST, unregistred user!
unit copyfile;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls, DBTables, DB;
type
TForm1 = class(TForm)
Button1: TButton;
ProgressBar1: TProgressBar;
Button2: TButton;
Edit1: TEdit;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

TCopyFileThread = class(TThread)
private
sSourceFileName, sTargetFileName: string;
SourceFile, TargetFile: TFileStream;
protected
procedure Execute;
override;
procedure CopyFile(Const sSourceFileName, sTargetFileName: string);
public
constructor Create();
virtual;
end;

var
Form1: TForm1;
implementation
{$R *.dfm}

{ CopyFileThread }
constructor TCopyFileThread.Create();
begin
FreeOnTerminate := True;
inherited Create(False);
end;

procedure TCopyFileThread.Execute;
begin
CopyFile(sSourceFileName,sTargetFileName);
end;

procedure TCopyFileThread.CopyFile(Const sSourceFileName, sTargetFileName: string);
begin
SourceFile := TFileStream.Create('e:/delphi7.rar', fmOpenRead);
try
TargetFile := TFileStream.Create('c:/delphi7.rar',fmOpenWrite);
try
TargetFile.CopyFrom(SourceFile, SourceFile.Size );
finally
TargetFile.Free;
end;
finally
SourceFile.Free;
end;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
TCopyFileThread.Create;
end;
 
这是多线程?我真的老了,居然没看出来。唉。
 
把下面这句中的fmOpenWrite改为fmCreate就可以了,至于为什么,你可以看一下
TFileStream.Create帮助
TargetFile := TFileStream.Create('c:/delphi7.rar',fmOpenWrite);
 
后退
顶部