Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 106867
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T01:39:51+00:00 2026-05-11T01:39:51+00:00

We have a C# application that connects to a FTP server, downloads some files,

  • 0

We have a C# application that connects to a FTP server, downloads some files, disconnects, and after a certain amount of time (selected by the user through the UI) reconnects and repeats the process. We implemented this using BackgroundWorker, but we noticed that after running for a longer time, the program stopped logging its actions, both in the UI and the log file. At that point, there were no files for it to download, so we uploaded some and it resumed activity as if nothing had happened.

The problem was that the regular users had no way of knowing that the program was still working, so we decided to implement it using our own threading. We did a simpler program, to rule out any other problems, and this one only connects to the FTP and disconnects. It stopped displaying messages just like BackgroundWorker (one time after 2 hours, one time after 22 hours, without any pattern that we could find, and on a computer that did nothing else).

DoFTPWork += new DoFTPWorkDelegate(WriteFTPMessage);  FTPWorkThread = new Thread(new ParameterizedThreadStart(Process));  //seData is the FTP login info FTPWorkThread.Start(seData); 

and the FTP method is:

private void Process(object seData1) {     seData = (SEData)seData1;     while (!stopped)     {         try         {             ftp = null;             ftp = new FTP_Client();              if (ftp.IsConnected)             {                 logMessages += DateTime.Now + '\t' + 'info' + '\t' + 'Ftp disconnected from ' + seData.host + '\r\n';                 ftp.Disconnect();             }              ftp.Connect(seData.host, 21);             ftp.Authenticate(seData.userName, seData.password);             logMessages += DateTime.Now + '\t' + 'info' + '\t' + 'Ftp connected to ' + seData.host + '\r\n';              error = false;             logMessages += DateTime.Now + '\t' + 'info' + '\t' + 'Trying to reconnect in 5 seconds\r\n';             System.Threading.Thread.Sleep(5000);             SlaveEventArgs ev = new SlaveEventArgs();             ev.Message = logMessages;             txtLog.Invoke(DoFTPWork, ev);             System.Threading.Thread.Sleep(200);             logMessages = '';         }          catch (Exception ex)         {             logMessages = '';             if (ftp.IsConnected)             {                 ftp.Disconnect();             }             ftp.Dispose();             logMessages += DateTime.Now + '\t' + 'ERR' + '\t' + ex.Message + '\r\n';              logMessages += DateTime.Now + '\t' + 'info' + '\t' + 'Trying to reconnect in 5 seconds\r\n';             SlaveEventArgs ev = new SlaveEventArgs();             ev.Message = logMessages;             txtLog.Invoke(DoFTPWork, ev);             System.Threading.Thread.Sleep(5 * 1000);             error = true;         }     } } 

WriteFTPMessage displays the message in a TextBox and in the original program wrote to a .txt file.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T01:39:52+00:00Added an answer on May 11, 2026 at 1:39 am

    If I’m understanding you correctly this while(!stopped) loop is the loop that is running for several hours? If that is the case, where are you terminating your ftp connection if anywhere? The only time you close it in the code you’ve posted is if an exception is thrown, otherwise you simply dereference the object and create a new one which is a pretty serious resource leak and at least contributing to the problem if not causing it.

    Also it seems that ftp is globally accessible. Are you accessing it anywhere using a different thread? Is the object thread safe??

    EDIT:

    The biggest issue I see here is design. Not that I’m trying to bag on you or anything but you’ve got all sorts of operations intermixed. Threading, logging and ftp access code all in the same function.

    What I would recommend is restructuring your program. Create a method much like the following:

    // Called by thread void MyThreadOperation() {    while(!stopped)    {       // This is poor design in terms of performance.       // Consider using a ResetEvent instead.       Thread.Sleep(5000);        try       {          doFTPDownload();       }       catch(Exception ex)       {          logMessage(ex.ToString());       }    } } 

    doFTPDownload() should be self contained. The FTP object should be created and opened inside the function when it is called and prior to it finishing it should be closed. This same concept should be applied to logMessage() as well. I would also recommend using a database to store log messages instead of a file so that locking issues don’t complicate matters.

    I know this isn’t an answer in that you may still experience issues since I can’t say for certain what could be the cause. However I’m confident with a little design restructuring you will be much better able to track down the source of the problem.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Flex application that connects to a BlazeDS server using the StreamingAMF
I have developed a j2me application that connects to my webhosting server through sockets.
I have an ASP .NET application that connects to an Oracle or a SQL
I currently have an MS Access application that connects to a PostgreSQL database via
I have created a timeclock application in C# that connects to a web service
In my application I have several DataContexts that connects to different databases with different
I have an application that connects via a DSN to an Oracle database. If
I have written an application that connects to a SSL web service (including client
I have written a winform application that connects to a database on our corporate
I have an .aspx page that connects to a remote application. I want to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.