Problems creating a progress bar for TidFTP

Giganews Newsgroups
Subject: Problems creating a progress bar for TidFTP
Posted by:  Burnett (raven_b…@hotmailDOTcom)
Date: Thu, 28 Apr 2005

Hi,

I simply want my app to upload a file to an FTP server. I created a thread
(TThread) within my main program and overrode the execute method. That all
worked fine, the file uploaded. I then wanted to add progress bar, I tried
using the OnWork events but the problem is my main program never recieves
the OnBegin and OnEnd events. It also only get one OnWork event that
contains the final byte count of the file being uploaded and this happens
before the upload is complete. OnStatus seems to work fine. Any ideas? I am
new to threads so I am sure its something dumb that I am doing.

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
My Test Code for Reference
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs,idFTP, StdCtrls, IdBaseComponent, IdComponent, IdTCPConnection,
  IdTCPClient, IdExplicitTLSClientServerBase, ComCtrls, IdAntiFreezeBase,
  IdAntiFreeze;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Memo1: TMemo;
    ProgressBar1: TProgressBar;
    IdFTP1: TIdFTP;
    procedure IdFTP1Status(ASender: TObject; const AStatus: TIdStatus;
      const AStatusText: String);
    procedure Button1Click(Sender: TObject);
    procedure IdFTP1WorkBegin(ASender: TObject; AWorkMode: TWorkMode;
      AWorkCountMax: Integer);
    procedure IdFTP1Work(ASender: TObject; AWorkMode: TWorkMode;
      AWorkCount: Integer);
    procedure IdFTP1WorkEnd(ASender: TObject; AWorkMode: TWorkMode);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

//-------- FTP Thread ------------------------------------------------
type
TFTPthread = class(TThread)
  private
  FTPClient: TidFTP;
protected
  procedure Execute; override;
  end;

Procedure TFTPthread.Execute;
begin
  FTPclient:= TidFTP.Create;
  with FTPClient do
  begin
    host:='ftp.somewhere.com';
    username:='username';
    password:='password';
    OnStatus:= Form1.IdFTP1Status;                    { Can I do this? }
    OnWorkBegin:= Form1.IdFTP1WorkBegin;
    OnWorkBegin:= Form1.IdFTP1Work;
  end;
  FTPClient.Connect;
  FTPClient.Put('myFile.txt','',true);
  FTPClient.Disconnect;
  FTPClient.Free;
end;
//--- FTP thread end

procedure TForm1.IdFTP1Status(ASender: TObject; const AStatus: TIdStatus;
  const AStatusText: String);
begin
  Memo1.Lines.Add(AStatusText);
end;

procedure TForm1.Button1Click(Sender: TObject);
var
FTP: TFTPthread;
begin
  FTP:=TFTPthread.Create(true);
  FTP.FreeOnTerminate:=true;
  FTP.Resume;
end;

procedure TForm1.IdFTP1WorkBegin(ASender: TObject; AWorkMode: TWorkMode;
  AWorkCountMax: Integer);
begin
  ProgressBar1.Max:= AWorkCountMax;
  ProgressBar1.Position:=0;
  Memo1.Lines.Add('Begin: ' +IntToStr(AWorkCountMax));
end;

procedure TForm1.IdFTP1Work(ASender: TObject; AWorkMode: TWorkMode;
  AWorkCount: Integer);
begin
  ProgressBar1.Position:=AWorkCount;
  Memo1.Lines.Add('Work: ' +IntToStr(AWorkCount));
end;

procedure TForm1.IdFTP1WorkEnd(ASender: TObject; AWorkMode: TWorkMode);
begin
  Memo1.Lines.Add('Work end');
end;

end.

Replies