Home Forums .NET libraries Xceed SFTP/FTP for .NET Is Xceed FTP .NET threadsafe?

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • User (Old forums)
    Member
    Post count: 23064
    #21083 |

    Hi all,

    we have an application which generates approx. 20 zipfiles which are in turn sent to an FTP server. This is done in a serial manner, so first the 1st file is generated and then sent to the desired FTP server, then the 2nd and so on. Because the target FTP servers are all different in URL or subdirectory it isn’t possible to send them in batch.

    To speed up things we changed the internal structure of the application so that it uses a threadpool and we create a new worker thread for each file to generate/FTP. The pool is now set to max. 3 threads.

    When I run the app. the generating of the files is ok but it goed haywire when the threads start to implement FTP. When two threads start to FTP at about the same time they seem to hang first. Then after ± 1 minute one of the threads get an FTP connect exception (code 426) and at that same time the other thread gets it’s connection, uploads the file and is finished.

    It has the signature of a deadlock where one thread ultimately is the victim. When I set the max. threads of the pool to 1 everything is ok.

    Is it possible to use Xceed in this manner in multiple threads? Or do the threads interfere each other at the FTP stage because Xceed isn’t threadsafe? 

    TIA,

    Friso Wiskerke

    Applies to Xceed FTP for .NET. Imported from legacy forums. Posted by Friso (had 5673 views)

    User (Old forums)
    Member
    Post count: 23064

    Are you using the same Ftp instance on each thread?  If so, this will not work.  You need to initialize a new FtpClient object for every thread.

    Applies to Xceed FTP for .NET. Imported from legacy forums. Posted by André (had 304 views)

    User (Old forums)
    Member
    Post count: 23064

    André,

    each thread has it’s own instance of the XCeed FTP object.

    Applies to Xceed FTP for .NET. Imported from legacy forums. Posted by Friso (had 657 views)

    User (Old forums)
    Member
    Post count: 23064

    First, we do not reproduce this on our side.  However, could you provide the code you’re using, to make sure we are doing things the same way?

    Applies to Xceed FTP for .NET. Imported from legacy forums. Posted by André (had 670 views)

    User (Old forums)
    Member
    Post count: 23064

    André,

    can you provide us the code you have used to try to reproduce the multithreading issue? maybe it can help us in the right direction….

    Applies to Xceed FTP for .NET. Imported from legacy forums. Posted by Friso (had 630 views)

    User (Old forums)
    Member
    Post count: 23064

    Hello Andre,

    We use this code:

    == Program.cs ==
    class Program
    {
        static void Main(string[] args)
        {
            ThreadPool.SetMaxThreads(2, 2);

            for (int i = 1; i < 7; i++ )
            {
                WorkerThread wt = new WorkerThread();
                ThreadPool.QueueUserWorkItem(new WaitCallback(wt.TransportViaFTP), string.Format(“d:\\file00{0}.rar”, i));
            }

            Console.ReadLine();
        }
    }

    == WorkerThread.cs ==
    internal class WorkerThread
    {
        internal void TransportViaFTP( object fileName )
        {
            try
           
    {
                Xceed.Ftp.Licenser.LicenseKey = “our key”;
                Xceed.Ftp.FtpClient oFTP = new Xceed.Ftp.FtpClient();

                oFTP.Connect(“ftp server”);
                oFTP.Login(“login”, “password”);

                oFTP.PassiveTransfer = true;
                oFTP.Timeout = 10;
                oFTP.KeepAliveInterval = 10;

                oFTP.ChangeCurrentFolder(“/Test”);

                oFTP.SendFile(fileName.ToString());
            }
            catch (Exception ex)
            {
                string message = ex.Message;
            }
        }
    }

    The exception is “The FTP command execution timed-out.”

    Applies to Xceed FTP for .NET. Imported from legacy forums. Posted by Friso (had 600 views)

    User (Old forums)
    Member
    Post count: 23064

    Ok, after further investigation, the problem is that we are also using the ThreadPool internally to send command to the server, and limiting it to only 2-3 threads concurrently prevents the call to SendFile, which results into the exception.

    So you basically have two choices.  Either do not limit the number of threads on the ThreadPool, or create your own 2-3 threads, and manage yourself the ftp transfer as each thread becomes available.

    Note that we have a new version coming out in February which we have modified in term of thread and asynchronous processing, and we have tested this with the beta version, and it works fine.  So you will be able to revert your code back when this version is released.  Note however that this will require to be a vanguard support subscriber to be able to get it free, else you will need to  buy an upgrade.  It will be version 4.0.  We can send the beta version to vanguard support subscribers.

    Applies to Xceed FTP for .NET. Imported from legacy forums. Posted by André (had 5163 views)

Viewing 7 posts - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.