Problems with TIdFtp

Giganews Newsgroups
Subject: Problems with TIdFtp
Posted by:  Staffan Bergbom (Staffan.Bergb…@lm.se)
Date: Fri, 15 Apr 2005

Hi all.

I have a problem getting an app using TIdFtp to run stable.

The app consists of

  a.. A polling-timer in the mainthread that triggers file-fetch from a
Ftp-server
  b.. A FtpRestart-timer to regularily restart the ftp-connection
  c.. A background-thread that does the actual file-fetch

When the polling-timer is triggered it is first disabled and then sends a
postthread-message to the background-thread that stands waiting for a
triggering-message. When the background-thread has performed a file-fetch it
posts a message the main-thread that enables the polling-timer again. The
FtpRestart-timer is just there to restart the ftp-connection, once every
hour, as it kind of locks-up irregularily.

The intention is to keep the connection to the Ftp-server up all times but
as it is a little shaky it will go down every now and then and in these
situations the app gets locked-up.

This can be followed in a log-file(se below Synchronize(AddToTrafficLog)).

The background-thread Execute-procedure looks:

procedure TFtpThread.Execute;

var

  TmpMsg : TMsg;

  temp : string;

  delayEvent : TEvent;

  StatusInfoObject : TStatusInfoObject;

  i, n : integer;

  sRec : TSearchRec;

begin

  inherited;

  frmDataMessengerFtpMain.FtpThreadId := getcurrentthreadid;

  peekmessage(TmpMsg,0,WM_USER,WM_USER + 7,PM_REMOVE); // force message
queue

  delayEvent := TEvent.Create(nil, false, false, '');

  FNumDownloadedReports := 0;

  FNumPreviouslyDownloadedReports := 0;

  repeat

    if frmDataMessengerFtpMain.ActivateLogging.Checked then

    begin

      FLogInfo := 'WaitForWindowsMessage';

      Synchronize(AddToTrafficLog);

    end;

    WaitMessage;

    getmessage(TmpMsg,0,0,0);

    if  TmpMsg.message = wm_user  then

    begin

// Retrieve reports

      if not FSQLConnection.Connected then

        if not ConnectToDatabase then Continue;

      if not FIdFTP.Connected  then

        if not ConnectToFtpServer then Continue;

      try

        DownloadReportsAndAddToDatabase;

      except

      end;

// enable mainthread-timer

      PostMessage(frmDataMessengerFtpMain.Handle, wm_user + 1, 0, 0);

      if Terminated then Break;

    end

    else if TmpMsg.message = wm_user + 7 then

    begin

// reset FIdFtp

      while FIdFTP.Connected do

      begin

        FActivity := 'Resetting Ftp-connection';

        Synchronize(ShowActivitiesIfChecked);

        try

          FIdFTP.Abort;

          FIdFTP.Disconnect;

          delayEvent.WaitFor(100);

          if FIdFTP.Connected then

            FIdFTP.DisconnectSocket;

          delayEvent.WaitFor(100);

        except

        end;

      end;

      ReadProxySettings;

// enable mainthread-timer

      PostMessage(frmDataMessengerFtpMain.Handle, wm_user + 1, 0, 0);

    end;

  until terminated;

Checking the log reveals:

2005-04-13 20:59:03 - WaitForWindowsMessage
// background-thread

2005-04-13 20:59:28 - Posting message "poll for reports" to FtpThread //
main-thread

2005-04-13 20:59:28 - Nothing to download...
// background-thread

2005-04-13 20:59:28 - WaitForWindowsMessage
// background-thread

2005-04-13 20:59:53 - Posting message "poll for reports" to FtpThread

2005-04-13 20:59:53 - Downloading 2 reports
// background-thread

2005-04-13 20:59:58 - 1303 downloaded
// background-thread

2005-04-13 20:59:58 - 0 download-errors
// background-thread

2005-04-13 20:59:58 - 1288 report(s) inserted
// background-thread

2005-04-13 20:59:58 - 15 insert-errors
// background-thread

2005-04-13 20:59:58 - 0 empty reports
// background-thread

2005-04-13 20:59:58 - WaitForWindowsMessage
// background-thread

2005-04-13 21:00:23 - Posting message "poll for reports" to FtpThread //
main-thread

2005-04-13 21:00:23 - Nothing to download...
// background-thread

2005-04-13 21:00:23 - WaitForWindowsMessage
// background-thread

2005-04-13 21:00:48 - Posting message "poll for reports" to FtpThread
// main-thread

--------At this point the polling timer don't seem to get enabled any more

2005-04-13 21:38:55 - Posting message "Reset Ftp-connection" to FtpThread //
main-thread

2005-04-13 22:38:55 - Posting message "Reset Ftp-connection" to FtpThread //
main-thread

2005-04-13 23:38:55 - Posting message "Reset Ftp-connection" to FtpThread //
main-thread

2005-04-14 00:38:55 - Posting message "Reset Ftp-connection" to FtpThread //
main-thread

2005-04-14 01:38:55 - Posting message "Reset Ftp-connection" to FtpThread //
main-thread

Is there anyone having a suggestion on how to keep the app running even if
there is broken connections sporadically?

Regards

Staffan

Replies