Archived Forum Post

Index of archived forum posts

Question:

Is it acceptable to pass an active FTP connection to a function?

Jul 15 '15 at 22:52

Hi. I am writing a C# FTP program. I'm a hobbyist PHP programmer, and I used to instantiate a PDO connection to the MySQL server in functions that needed SQL access. I then read that it's better to pass the already open PDO object to the function.

I was curious, is that the same with the FTP object? I'm doing this (a bit pared down but gets the gist):

using (Chilkat.Ftp2 ftp = new Ftp2()) {
  if (ftp.Connect()) {
    Targets.Clear(); DownloadQueue.Clear();
    Write("Scanning accounting projects ...");
    ProcessTargets(ftp, GetChildren(ftp, "/Projects/Acct"));

    Write("Scanning engineering projects ...");
    ProcessTargets(ftp, GetChildren(ftp, "/Projects/Eng"));

    Write("Scanning sales projects ...");
    ProcessTargets(ftp, GetChildren(ftp, "/Projects/Sales"));

    if (DownloadQueue.Count > 0) {
      trd = new Thread(new ThreadStart(DownloadThread));
      trd.Start();
    }

    ftp.Disconnect();
  }
}

void ProcessTargets(Chilkat.Ftp2 ftp, ArrayList targets) {
  try {
    foreach (string target in targets) {
      Write("Processing " + target + " ...");
      (blah blah ftp stuff)
      Application.DoEvents();
    }
  } catch (Exception ex) {
    Write("** EXCEPTION (PT): " + ex.Message);
  }
}

Is that proper? Thanks!


Answer

I don't think there should be any problem passing around a reference to an FTP object with an connection, at least as long as the destination function is running in the same thread as where you created the FTP object.

There might be an issues with you DoEvents call though if you are performing other actions against the same FTP object in other events - they may get fired and produce unexpected results.

Chilkat would have a definitive answer though, so if you have an active support license, it might be worth checking in through the official support channel.