unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, IdBaseComponent, IdComponent, IdTCPConnection,
  IdTCPClient, IdHTTP, ComCtrls;

  const
  CFileName = 'D:\serverlist.txt';
  CUrl = 'http://ver.bo.sohu.com/version/serverlist.txt';
  CBlock = 1024;

  type
  TForm1 = class;
  TDownThread = class(TThread)
  private
    FOwner: TForm1;
  protected
    procedure Execute; override;
  public
    constructor Create(AOwner: TForm1);
  end;
  TForm1 = class(TForm)

    Button1: TButton;
    IdHTTP1: TIdHTTP;
    IdTCPClient1: TIdTCPClient;
    Button2: TButton;
    Edit1: TEdit;
    ProgressBar1: TProgressBar;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure IdHTTP1WorkEnd(Sender: TObject; AWorkMode: TWorkMode);
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    FThread: TDownThread;
    FFile: TFileStream;
    FFileSize: Integer;
    { Private declarations }
  protected
    procedure Execute;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}
{ TDownThread }

constructor TDownThread.Create(AOwner: TForm1);
begin
  inherited Create(True);
  FOwner := AOwner;
end;

procedure TDownThread.Execute;
begin
  while not Terminated do
    FOwner.Execute;
end;

{ TForm1 }

procedure TForm1.Execute;
begin
  FFile.Position := FFile.Size;
  IdHttp1.Request.ContentRangeStart := FFile.Position;
  IdHttp1.Request.ContentRangeEnd := IdHttp1.Request.ContentRangeStart + CBlock;
  if IdHttp1.Request.ContentRangeEnd > FFileSize then
    IdHttp1.Request.ContentRangeEnd := FFileSize;
  IdHttp1.Get(CUrl, FFile);
  if FFile.Size = FFileSize then
    FThread.Terminate;
end;




procedure TForm1.Button1Click(Sender: TObject);
begin
FThread.Resume;

end;

procedure TForm1.Button2Click(Sender: TObject);
begin
FThread.Suspend;

end;

procedure TForm1.IdHTTP1WorkEnd(Sender: TObject; AWorkMode: TWorkMode);
begin
ProgressBar1.Position := FFile.Position;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
if FileExists(CFileName) then
    FFile := TFileStream.Create(CFileName, fmOpenWrite)
  else
    FFile := TFileStream.Create(CFileName, fmCreate);

  IdHttp1.Head(CUrl);
  FFileSize := IdHttp1.Response.ContentLength;
  ProgressBar1.Min := 0;
  ProgressBar1.Max := FFileSize;
  ProgressBar1.Position := FFile.Size;
  FThread := TDownThread.Create(Self);
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  FThread.Free;
  FFile.Free;
end;

end.
posted on 2010-01-17 16:36  oKmAn.Org  阅读(269)  评论(0编辑  收藏  举报