Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
ProgressBar while FTP retreive
#6
The issue you're experiencing with the progress bar not updating during the FTP file transfer process might be due to blocking the UI thread. The FTP Get operation is likely blocking the UI thread, preventing it from updating the progress bar.

To address this issue, you can perform the FTP file transfer operation in a separate thread or task, allowing the UI thread to continue updating the progress bar. Here's an example of how you can achieve this using TThread:

Code:
procedure TForm1.DownloadFile(const AHost, AUsername, APassword, ARemoteFile, ALocalFile: string);
var
  FTP: TIdFTP;
begin
  FTP := TIdFTP.Create(nil);
  try
    FTP.Host := AHost;
    FTP.Username := AUsername;
    FTP.Password := APassword;
   
    // Set up event handlers to track progress
    FTP.OnWork := FTPWork;
    FTP.OnWorkBegin := FTPWorkBegin;
    FTP.OnWorkEnd := FTPWorkEnd;

    // Connect and download the file in a separate thread
    TThread.CreateAnonymousThread(
      procedure
      begin
        try
          FTP.Connect;
          FTP.Get(ARemoteFile, ALocalFile);
        finally
          FTP.Disconnect;
        end;
      end
    ).Start;

    // Show the progress bar and start updating it
    ProgressBar1.Visible := True;
    ProgressBar1.Position := 0;

    // Wait for the download to complete
    while not FTP.Connected do
      Sleep(100);

    // You can perform any post-download actions here

  finally
    FTP.Free;
  end;
end;

procedure TForm1.FTPWork(ASender: TObject; AWorkMode: TWorkMode; AWorkCount: Int64);
begin
  // Update the progress bar based on the current work count
  ProgressBar1.Position := AWorkCount;
  Application.ProcessMessages; // Allow UI updates
end;

procedure TForm1.FTPWorkBegin(ASender: TObject; AWorkMode: TWorkMode; AWorkCountMax: Int64);
begin
  // Set the progress bar's maximum value
  ProgressBar1.Max := AWorkCountMax;
end;

procedure TForm1.FTPWorkEnd(ASender: TObject; AWorkMode: TWorkMode);
begin
  // Hide the progress bar when the FTP work ends
  ProgressBar1.Visible := False;
end;

In this example, the DownloadFile procedure sets up an FTP connection and starts the download in a separate thread using TThread.CreateAnonymousThread. The progress bar is updated in the FTPWork event handler, which is called during the FTP operation. The FTPWorkBegin event handler sets the maximum value of the progress bar, and the FTPWorkEnd event handler hides the progress bar when the FTP work is complete.

Make sure to replace the FTP connection details (AHost, AUsername, APassword) and the file paths (ARemoteFile, ALocalFile) with your specific values.

By executing the FTP operation in a separate thread, the UI thread remains responsive and can continue updating the progress bar.
Reply


Messages In This Thread
ProgressBar while FTP retreive - by zsleo - 05-18-2023, 03:51 AM
RE: ProgressBar while FTP retreive - by rlebeau - 05-18-2023, 07:54 PM
RE: ProgressBar while FTP retreive - by zsleo - 05-19-2023, 03:11 AM
RE: ProgressBar while FTP retreive - by rlebeau - 05-19-2023, 04:25 PM
RE: ProgressBar while FTP retreive - by zsleo - 05-22-2023, 06:04 AM
RE: ProgressBar while FTP retreive - by Neerarawat - 06-22-2023, 03:51 PM
RE: ProgressBar while FTP retreive - by rlebeau - 06-22-2023, 05:19 PM

Forum Jump:


Users browsing this thread: 2 Guest(s)