//主程序单元
unit Unt_Main;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls, , ExtCtrls;
type
TForm1 = class(TForm)
procedure Button1Click(Sender: TObject);//释放化开程
private
{ Private declarations }
public
{ Public declarations }
C: TC; //声明读取数据的线程
end;
var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.N1Click(Sender: TObject);
var
s:string;
begin
C:= TC.Create(False);//创建拷贝线程
end;procedure TForm1.Button1Click(Sender: TObject);//释放化开程
begin
C.Free; //释放对象
end;procedure TForm1.ShowCopyWindow;
begin
Form6:= TForm6.Create(self);
Form6.Show ;//显示拷贝的窗口单元
end;procedure TForm1.CloseCopyWindow;
begin
Form6.Close ;//关闭拷贝的窗口单元
end;//========================================================
//线程单元
unit Unt_C;interfaceuses
Classes, SysUtils;type
TC = class(TThread)
private
{ Private declarations }
protected
procedure Execute; override;
procedure CopyFile(DestFileName, SourceFileName: String);
procedure a;
end;implementationuses
Unt_Main,Unt_ShowCopyWindow;{ Important: Methods and properties of objects in VCL or CLX can only be used
in a method called using Synchronize, for example, Synchronize(UpdateCaption); and UpdateCaption could look like, procedure TVCDCopy.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end; }{ TC }procedure TC.a;
begin
CopyFile(Unt_Main.DestFileName,Unt_Main.SourceFileName );
end;procedure TC.CopyFile(DestFileName, SourceFileName: String);
var
f1,f2: TFileStream ;
begin
f1:=Tfilestream.Create(SourceFileName,fmShareDenyRead);
try
f2:=Tfilestream.Create(DestFileName,fmOpenWrite or fmCreate);
try
f2.CopyFrom(f1,f1.size);//拷贝代码,必须全部拷完才执行下一步操作.
finally
f2.Free;
end;
finally
f1.Free;
end;end;procedure TC.Execute;
begin
{ Place thread code here }
//Synchronize
(a);
end;end.
每当执行到C.Free; //释放对象时就死在那里,cpu使用100%,请问各位高手如何解决。
unit Unt_Main;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls, , ExtCtrls;
type
TForm1 = class(TForm)
procedure Button1Click(Sender: TObject);//释放化开程
private
{ Private declarations }
public
{ Public declarations }
C: TC; //声明读取数据的线程
end;
var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.N1Click(Sender: TObject);
var
s:string;
begin
C:= TC.Create(False);//创建拷贝线程
end;procedure TForm1.Button1Click(Sender: TObject);//释放化开程
begin
C.Free; //释放对象
end;procedure TForm1.ShowCopyWindow;
begin
Form6:= TForm6.Create(self);
Form6.Show ;//显示拷贝的窗口单元
end;procedure TForm1.CloseCopyWindow;
begin
Form6.Close ;//关闭拷贝的窗口单元
end;//========================================================
//线程单元
unit Unt_C;interfaceuses
Classes, SysUtils;type
TC = class(TThread)
private
{ Private declarations }
protected
procedure Execute; override;
procedure CopyFile(DestFileName, SourceFileName: String);
procedure a;
end;implementationuses
Unt_Main,Unt_ShowCopyWindow;{ Important: Methods and properties of objects in VCL or CLX can only be used
in a method called using Synchronize, for example, Synchronize(UpdateCaption); and UpdateCaption could look like, procedure TVCDCopy.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end; }{ TC }procedure TC.a;
begin
CopyFile(Unt_Main.DestFileName,Unt_Main.SourceFileName );
end;procedure TC.CopyFile(DestFileName, SourceFileName: String);
var
f1,f2: TFileStream ;
begin
f1:=Tfilestream.Create(SourceFileName,fmShareDenyRead);
try
f2:=Tfilestream.Create(DestFileName,fmOpenWrite or fmCreate);
try
f2.CopyFrom(f1,f1.size);//拷贝代码,必须全部拷完才执行下一步操作.
finally
f2.Free;
end;
finally
f1.Free;
end;end;procedure TC.Execute;
begin
{ Place thread code here }
//Synchronize
(a);
end;end.
每当执行到C.Free; //释放对象时就死在那里,cpu使用100%,请问各位高手如何解决。
don't use free
add FreeOnTerminate to thread constructor