Our Products:   CompleteFTP  edtFTPnet/Free  edtFTPnet/PRO  edtFTPj/Free  edtFTPj/PRO
0 votes
1.7k views
in .NET FTP by (120 points)
Here is the code:

using System;
using System.Linq;
using EnterpriseDT.Net.Ftp;

namespace ftpClient
{
    internal class Program
    {
        static void Main()
        {
            var con = new SecureFTPConnection
            {
                Protocol = FileTransferProtocol.FTP,
                LicenseOwner = "***",
                LicenseKey = "***",
                ServerAddress = "ftp.swfwmd.state.fl.us", 
                ServerDirectory = "pub",
                UserName = "anonymous", 
                Password = "a@b.c"
            };

            var ftpTask = con.BeginConnect(null, null) as FTPTask;

            try
            {
                if (!ftpTask.AsyncWaitHandle.WaitOne(TimeSpan.FromMilliseconds(100), false))
                {
                    // The connection has timed out. Abort the task just created.
                    var isConnected = con.IsConnected;
                    var isCompleted = ftpTask.IsCompleted;
                    ftpTask.Cancel();
                    Console.WriteLine("Attempted to cancel: con.IsConnected = {0}, ftpTask.IsCompleted = {1}", isConnected, isCompleted);
                }
                else
                {
                    con.EndConnect(ftpTask);
                }
            }
            catch (Exception exc)
            {
                Console.WriteLine(exc);
                //ftpTask.AsyncWaitHandle.Close();
            }

            var files = con.GetFileInfos().Take(10);
            foreach (FTPFile file in files)
            {
                Console.Out.WriteLine(file.Name);
            }
            con.Close();
            Console.WriteLine("Press any key ...");
            Console.ReadKey();
        }
    }
}


Running it outputs the following:

Attempted to cancel: con.IsConnected = False, ftpTask.IsCompleted = False
CFCA
CFWI_PUB_COST_Docs
ECFT_Model_From_SFWMD
GWIS
Lake_Hancock_Field_Office_Project
PRINTSHOP
README.txt
RRWPI
RWSP
amr
Press any key ...


For the life of me I do not understand why? I expected it to fail, because I supposedly cancelled it, didn't I?

What is wrong?

Thank you.

P.S.
I have also posted it on SO - http://stackoverflow.com/questions/2491 ... cureftpcon

1 Answer

0 votes
by (51.6k points)
Connecting is not a regular task and can't be cancelled in this way. Use Close(true) instead as this will close the socket forcefully. Also, you should be able to use the Timeout property to achieve the same effect. This will also allow you to call the synchronous method, Connect, which will simplify your code.

- Hans (EnterpriseDT)

Categories

...