Ftp provides a number of methods allowing you to asynchronously connect, login, upload, download, etc. Their names always end with Async. This means the method will begin some operation on a new thread and immediately return to the caller. When the operation has completed, the corresponding event will be raised notifying you that the operation has completed and in the event handler method you can check for the error and use the result, if provided.
The example following demonstrates how to connect to an FTP server and download a file asynchronously:
using System; using System.IO; using ComponentPro.IO; using ComponentPro.Net; using ComponentPro; ... // Create a new class instance. Ftp client = new Ftp(); // Connect to the FTP server. client.Connect("localhost"); // Authenticate. client.Authenticate("test", "test"); // ... // Download remote file '/test.dat' to 'c:\test.dat' long transferred = await client.DownloadFileAsync("/test.dat", "c:\\test.dat"); // ... Console.WriteLine("Bytes transferred: " + transferred); // Disconnect. client.Disconnect();
using System; using System.IO; using ComponentPro.IO; using ComponentPro.Net; using ComponentPro; ... static void Main() { // Create a new class instance. Ftp client = new Ftp(); // Connect to the FTP server. client.Connect("localhost"); // Authenticate. client.Authenticate("test", "test"); // ... // Register an event handler. client.DownloadFileCompleted += client_DownloadFileCompleted; // Download remote file '/test.dat' to 'c:\test.dat' client.DownloadFileAsync("/test.dat", "c:\\test.dat"); // ... // Disconnect. client.Disconnect(); } static void client_DownloadFileCompleted(object sender, ExtendedAsyncCompletedEventArgs<long> e) { // Ftp client = (Ftp)sender; if (e.Error != null) Console.WriteLine("Error: " + e.Error.Message); else { long transferred = e.Result; Console.WriteLine("Bytes transferred: " + transferred); } }