GetPutAsync - asynchronous SFTP download/upload

A utility for uploading and downloading files asynchronously using FTP.

GetPutAsync sample (both C# and VB.NET) is identical to GetPut sample, but transfers the files asynchronously in the background.

All methods of the Ftp class have the asynchronous versions that behave according to .NET asynchronous methods design patterns.

Sample codes for .NET 2.0

C#

IAsyncResult asyncResult = sftp.BeginGetFile(
    remotePath,
    localPath,
    new AsyncCallback(MyCallback),
    null);

while (!asyncResult.IsCompleted)
{
    // do something else here ...
}

// finish asynchronous operation and get the result
long bytes = sftp.EndGetFile(asyncResult);

VB.NET

' begin asynchronous transfer
Dim asyncResult As IAsyncResult
asyncResult = sftp.BeginGetFile( _
    remotePath, _
    localPath, _
    New AsyncCallback(AddressOf MyCallback), _
    Nothing)

Do
    ' do something else here ...
Loop While Not asyncResult.IsCompleted

' get the result
Dim bytes As Long
bytes = sftp.EndGetFile(asyncResult)

The MyCallback method is called when the transfer is finished.

Sample codes for .NET 4.0

C#

Task<long> operation = sftp.GetFileAsync(remotePath, localPath);

while (!operation.IsCompleted)
{
    // do something else here
}

// read result
long bytes = operation.Result;

VB.NET

' begin asynchronous transfer
Dim operation As Task(Of Long) = sftp.GetFileAsync(remotePath, localPath)
Do
    ' do something else here ...
Loop While Not operation.IsCompleted

' get the result
Dim bytes As Long = operation.Result