+1 vote

Hi,
Could you provide C# samples for multi threading upload through Ftp.dll?
Multi threading in following way:
1. Separate file in multi thread upload / download.
Means with each thread a separate file being Uploaded OR Downloaded

Thanks in advance.

Regards

by

1 Answer

0 votes
 
Best answer

Generally FTP protocol doesn't have this feature as there is a single command and a single data connection.

You can however create multiple Ftp instances in separate threads and split download or upload jobs between them.

string[] files = new[] { 
    "test1.txt", "test2.txt", 
    "test3.txt", "test4.txt" 
};

ConcurrentDictionary<string, byte[]> downloads =
    new ConcurrentDictionary<string, byte[]>();

List<Task> tasks = new List<Task>();
foreach (string file in files)
{
    tasks.Add(Task.Run(() =>
    {
        using (Ftp ftp = new Ftp())
        {
            ftp.ConnectSSL("ftp.example.com);
            ftp.Login("user", "password");
            byte[] downloaded = ftp.Download(file);
            downloads[file] = downloaded;
        }
    }));
}

Task.WaitAll(tasks.ToArray());

Assert.AreEqual("1", downloads["test1.txt"]);
Assert.AreEqual("2", downloads["test2.txt"]);
Assert.AreEqual("3", downloads["test3.txt"]);
Assert.AreEqual("4", downloads["test4.txt"]);
by (300k points)
selected by
...