FTP Begin*** / End*** methods

Could you provide me with an example of how the Begin and End Asynchronous methods are supposed to be used? I've tried to implement them, but I'm not getting the results that I expected. Thanks!

Dan

It's been over a day, could I please get a response to this? Thanks!

Dan

Hello Dan,

Please treat Begin** and End** as auto threaded operations:

static Aspose.Network.Ftp.FtpClient client;
public static void testDownload()
{
client = new Aspose.Network.Ftp.FtpClient("192.168.18.100", 21, "anonymous", "test");
try
{
client.Connect(true);
client.BeginDownload("\\temp.data", "c:\\temp.data", null, null);
while(true)
{
//do other things or exit this function, download will continue
System.Threading.Thread.Sleep(1000);
Console.WriteLine("waiting");
}
}
catch(Exception ex)
{
}
}

//event handler
private static void client_TransferCompleted(object sender, Aspose.Network.Ftp.TransferCompletedEventArgs e)
{
Console.Write("download complete");
client.Disconnect();
}

Thanks.

If I am downloading 5 files, do I need to call client.EndDownload after each file, or is that only used to end a download prematurally? From your example it looks like client.Disconnect() will close out any sessions opened by BeginDownload.

Thanks.

Dan

I followed your example code but all I get is a 0 (zero) KB file. It never downloads any data. I have more than one file to download, but it just sits on the first file and never does anything. My code is below.

Imports aFtp = Aspose.Network.Ftp

Private WithEvents _ftp As aFtp.FtpClient

Private Sub download()

If Not _ftp.Connected Then

_ftp = New Aspose.Network.Ftp.FtpClient("myHost", "username", "password")

_ftp.Connect(True)

End If

_ftp.ChangeDirectory("downloadDirectory")

_ftp.BeginDownload(remoteFile, localFilePath, Nothing, Nothing)

End Sub

Private Sub _ftp_TransferProgress(ByVal sender As Object, ByVal e As Aspose.Network.Ftp.TransferProgressEventArgs) Handles _ftp.TransferProgress

'Show progress visually

End Sub

Private Sub _ftp_TransferBroken(ByVal sender As Object, ByVal e As Aspose.Network.Ftp.TransferBrokenEventArgs) Handles _ftp.TransferBroken

'Handle broken download

End Sub

Private Sub _ftp_TransferCompleted(ByVal sender As Object, ByVal e As Aspose.Network.Ftp.TransferCompletedEventArgs) Handles _ftp.TransferCompleted

'Download the next file

End Sub

Hello Dan,

Thanks you for helping us to improve our products.

The codes you've posted here is absolutely correct. It's our problem. Yet another bug found, that's good news. The problem is the local file stream accidentally be closed before download happens, hence your symptom. We will provide hotfix tonight, sorry for the inconvenience.

For your infomation, what Begin*** and End*** method does is to BeginInvoke and EndInvoke something, such as invoking Download method in the samples above, which means posting a message (NOT sending a message) to current thread's measage pool, when this message is processed by current thread, download method is called. Current thread and download method are using the same message pool (queue).
Posting a message does not block the thread who posts it. That means current thread does not have to stop to wait for that message to be processed.

In asyncronized operations, we normally need a callback to retrieve related infomations, however, in FtpClient, there are already events: TransferProgress, TransferCompleted and TransferBroken doing this job. So you don't need to worry about calling EndDownload() anyhow.

Dear Dan,

I have created a demo about the asynchronousing download files using FtpClient. And what's more a new hotfix is released for the bug I mentioned before. Please download the latest hotfix from the web site and review the sample code. Feel free to ping me with any problems.

[STAThread]
static void Main(string[] args)
{
DownloadTask task = new DownloadTask();
task.Run();
}

public class DownloadTask
{
public DownloadTask()
{
this.fileList = new string[] {"file001.zip", "file002.zip", "file003.zip", "file004.zip", "bigfile.zip"};
this.manualEvent = new ManualResetEvent(false);//set the event
this.client = new FtpClient();
this.incompletdCount = fileList.Length;
this.async = new object();
this.client.TransferCompleted += new TransferCompletedEventHandler(TransferCompletedEventHandler); //register the transfer completed envent
}

public void Run()
{

client.Connect(true);
for(int i = 0; i < fileList.Length ; i++)
{
System.Diagnostics.Debug.WriteLine("begin downloading "+ fileList[ i ]);
client.BeginDownload(fileList[ i ], "local"+fileList[ i ], null, null);//fire the download

}
manualEvent.WaitOne();//wait all the download completed
System.Diagnostics.Debug.WriteLine("download task completed");
}

void TransferCompletedEventHandler(object sender, TransferCompletedEventArgs e)
{
System.Diagnostics.Debug.WriteLine(e.RemoteTargetPath + "is download completed");

lock (async)
{
this.incompletdCount --;
if (incompletdCount <= 0)//to see if the download completed
{
manualEvent.Set();//set the event to quite the main thread
}
}

}

object async;
int incompletdCount;
string [] fileList;
private FtpClient client;
ManualResetEvent manualEvent;
}

Thanks for the code examples and the quick fix on this bug! I'm going to go download the hotfix right now.

Dan