views:

126

answers:

4

I have an application implemented with BackgroundWorker that periodically connects to ftp (for download and upload) using LumiSoft library. It works just fine but after a while (let's say 2 - 3 hours) the thread (backgroundworker) blocks under the Connect method (witch doesn't throw any exception). Here is the part of code that fails: try {

            ftp = new FTP_Client();
            logMessages = "";
            logMessages += Utils.FormatLogMessage("Ftp wants to connect " +
                seData.host, Utils.MessageType.Info);
            worker.ReportProgress(0);

            ftp.Connect(seData.host, 21);
            ftp.Authenticate(seData.userName, seData.password);

            logMessages = "";
            logMessages += Utils.FormatLogMessage("Ftp connected to " +
                seData.host, Utils.MessageType.Info);
            error = false;
            worker.ReportProgress(0);
            System.Threading.Thread.Sleep(200);
            logMessages = "";
        }
        catch (Exception ex)
        {
            logMessages = "";
            if (ftp.IsConnected)
            {
                ftp.Disconnect();
            }
            ftp.Dispose();
            logMessages += Utils.FormatLogMessage(ex.Message,  Utils.MessageType.Error);
            logMessages += Utils.FormatLogMessage("Trying to reconnect in " +
                seData.recTime.ToString() + " seconds", Utils.MessageType.Info);
            worker.ReportProgress(0);
            Beep(500, 500);
            System.Threading.Thread.Sleep(seData.recTime * 1000);
            error = true;
        }

This is executed at let's say every 2 minutes. So it blocks on "ftp.Connect(seData.host, 21);" and that only happens after a while. Any idea why this is happening? Please let me know if you have any suggestions. Thanks. Dana.

A: 

does it close the port after itself? If not, perhaps it exceeds the maximum avaiable network ports.

jhornnes
+2  A: 

You don't appear to be disposing of the FTP_Client object unless an exception is caught? Or did you leave out some code?

Mitch Wheat
A: 

@Mitch Wheat I am disposing FTP_Client every time before that try/catch. I just left out that part of the code

A: 

@jhornnes I don't think that's what it is. Sometimes it runs even for 2 days(connecting and disconnecting)