当文件正在下载中的时候点'取消'按钮,'下载'按钮变为'重新下载',线程终止,
nowUpdata.Terminate;过程能不能终止线程取决于你的下载的代码
一般下载时用 while not Terminated do
begin
....
end;
因为nowUpdata.Terminate只是设定设定Terminated = true;所以从你的问题看八成是线程更本没有
中止,打开的文件没有关闭
{$R *.dfm}
var
nowUpdata : Updata;//线程
//------------------------------------------------------------------------------
//窗体初始化
//------------------------------------------------------------------------------
procedure TForm1.FormCreate(Sender: TObject);
var
TempPath : array[0..255] of char;
Tpath : string;
ini : TInifile;
begin
RevedByte := TBindStatusCallback.Create;//控制进度条.
Label1.Caption := '点击┏更新┓按钮开始升级';
Label2.Visible := false;
finish := false;
finished := false;
GetTempPath( 256, TempPath );
Tpath := TempPath;
Tpath := Trim( Tpath );
if Copy( Tpath, Length(Tpath), 1 ) <> '\' then
Tpath := Tpath + '\';
ServerIniFile := Tpath + 'update.ini';
LocalIniFile := Extractfilepath( Application.exename ) + 'Predate.ini';
if not FileExists( LocalIniFile ) then
begin
ini := TInifile.Create( LocalIniFile );
ini.WriteString('Message','Update','');
ini.WriteDateTime('Message','Update',strtodatetime('2003-2-12 4:00:12'));
ini.Free;
end;
end;
//------------------------------------------------------------------------------
//读取更新信息
//------------------------------------------------------------------------------
function TForm1.ReadUpdateMessage( inifile: string;
var UpdateLst: Tstrings ) : Boolean;
var
ini, Mini : TIniFile;
SectionLst : TStrings;
str : string;
CRC32 : LongInt;
allStr,Source,Dest : string;
i ,j : integer;
UpDate,NowDate : TdateTime;
procedure Error;
begin
Application.MessageBox('Open error.', 'I/O Error', mb_Ok or mb_IconStop)
end;
begin
Result := false;
j := 1;
Mini := TInifile.Create( LocalIniFile );
UpDate := strtodatetime( Mini.ReadString('Message','UpDate','') );
ini := TInifile.Create( ServerIniFile );
NowDate := strtodatetime( ini.ReadString('Time','UpDateTime','') );
if Update >= NowDate then
begin
Mini.Free;
ini.Free;
finish := true;
finished := true;
nowUpdata.Terminate;
ResumeFrm;
Exit;
end;
SectionLst := TStringList.Create;
ini.ReadSection( SectionStr, SectionLst );
filecount := SectionLst.Count;
for i := 1 to filecount do
begin
Label1.Caption := format( '正在下载第%d个更新文件:',[i]);
allStr := ini.ReadString( SectionStr,SectionLst.Strings[i-1],'' );
Source := MainUrl + allstr;
Dest := ExtractFilePath( Application.ExeName ) + allstr;
if Not UpdateFile( Source, Dest ) then
begin
Result := false;
Label1.Caption := '更新失败...';
Label2.Visible := false;
suiProgressbar1.Position := 0;
CrThread := false;
suiButton1.Enabled := true;
finish := false;
exit;
end
else //crc校验
begin
str := ini.ReadString( 'Crc32', inttostr(j), '' );
if not FileCRC32( Dest, CRC32 ) then
begin
Error;
Exit;
end
else
begin
if inttostr(CRC32) = str then
Result := true
else
begin
Result := false;
Application.MessageBox( '文件CRC错误,请重新下载.',
'Error',
mb_Ok or
mb_IconStop);
Label1.Caption := '更新失败...';
Label2.Visible := false;
suiProgressbar1.Position := 0;
CrThread := false;
suiButton1.Enabled := true;
finish := false;
exit;
end;
end;
end;
CurDown := i;
end;
Mini.WriteString( 'Message','Update',datetimetostr(Nowdate) );
Mini.Free;
ini.Free;
nowUpdata.Terminate;
ResumeFrm;
end;
//------------------------------------------------------------------------------
//更新完成后的窗体
//------------------------------------------------------------------------------
procedure TForm1.ResumeFrm;
begin
CrThread := false;
suiButton1.Enabled := true;
if FileExists( ServerIniFile ) then
Deletefile( ServerIniFile );
if finished = true then
label1.Caption := '已经更新完毕...'
else
begin
Label1.Caption := '升级完成,谢谢使用...';
end;
suiButton2.Caption := '退出';
if GetForeGroundWindow <> Application.Handle then
SetForeGroundWindow( Application.Handle );
end;
//------------------------------------------------------------------------------
//下载文件
//------------------------------------------------------------------------------
function TForm1.UpdateFile( Source, Dest: string ) : Boolean;
begin
if UrlDownloadToFile( nil,
Pchar(Source),
Pchar(Dest),
0,
RevedByte ) = 0 then
Result := True
else
Result := False;
end;