Xceed .NET Libraries Documentation
AsyncFtpClient Class
Members  Example 


Xceed.Ftp Assembly > Xceed.Ftp Namespace : AsyncFtpClient Class

AsyncFtpClient's methods now call the corresponding synchronous methods on a background thread. It is therefore recommended to use FtpClient and assign a SynchronizingObject to its SynchronizingObject property to improve code readability. For this reason, the AsyncFtpClient class is now considered obsolete.

This class provides you the with same functionalities that you will find in the FtpClient class but also gives you access to asynchronous FTP functionalities.

Syntax
'Declaration
 
Public Class AsyncFtpClient 
   Inherits FtpClient
'Usage
 
Dim instance As AsyncFtpClient
public class AsyncFtpClient : FtpClient 
Remarks

Xceed FTP for .NET is a highly multi-threaded library that uses asynchronous operations on sockets and network streams to allow fast and robust execution. This results in most events being called from a different thread than the one that called the initiating method.

When building a GUI application using System.Windows.Forms, UI elements (controls and forms) must always be accessed from the main thread since they have thread-affinity for the main STA thread. This is done using Control.Invoke or Control.BeginInvoke.

To avoid the need to call Control.Invoke or Control.BeginInvoke, the FtpClient.SynchronizingObject property can be set to any object implementing the System.ComponentModel.ISynchronizeInvoke interface. In doing so, the library will take care of raising the events on the thread of that object.

In order to avoid cross-blocking calls, the library will call System.Windows.Forms.Application.DoEvents to pump the messages on the main thread when using blocking methods. When using the asynchronous methods, the library will not pump messages for you. When using a callback, there is no need to pump the messages since the execution is not blocked. When waiting for the completion of the operation, messages must be pumped manually by calling System.Windows.Forms.Application.DoEvents

Example
This example demonstrate how to use the library by using the BeginXyz method with a callback and calling EndXyz when the callback is called. This example demonstrate how to use the library by using the BeginXyz method with a callback and calling EndXyz when the callback is called. This example demonstrate how to use the library by using the BeginXyz method with a callback and calling EndXyz when the callback is called. This example demonstrate how to use the library by using the BeginXyz method then pumping messages as long as the returned IAsyncResult's System.IAsyncResult.IsCompleted property is false. This example demonstrate how to use the library by using the BeginXyz method then pumping messages as long as the returned IAsyncResult's System.IAsyncResult.IsCompleted property is false.
Dim client As New AsyncFtpClient()

' You can instruct the library to automatically redirect events on the main UI thread
' by setting the SynchronizingObject property.
client.SynchronizingObject = m_resultForm

Try
    ' Start connecting asynchronously.
    client.BeginConnect("ftp.winzip.com", New AsyncCallback(AddressOf ConnectCompleted), client)
Catch except As Exception
    Console.WriteLine(except.Message)
End Try

Private Sub ConnectCompleted(result As IAsyncResult)
    Dim client As AsyncFtpClient = result.AsyncState
    
    Try
        ' All Begin calls must be matched by their End call.
        client.EndConnect(result)
        
        ' Now that we are connected, let's login anonymously, but again
        ' using the async method call.
          client.BeginLogin(New AsyncCallback(AddressOf LoginCompleted), client)
    Catch except As Exception
        Console.WriteLine(except.Message)
    End Try
End Sub

Private Sub LoginCompleted(result As IAsyncResult)
    Dim client As AsyncFtpClient = result.AsyncState
    
   Try
       ' All Begin calls must be matched by their End call.
       client.EndLogin(result)
       
       ' Finally, we disconnect asynchronously. You can see that if you
       ' wish to perform more operations, a cascading series of async
       ' calls can get out of hand.
       client.BeginDisconnect(New AsyncCallback(AddressOf DisconnectCompleted), client)
   Catch except As Exception
       Console.WriteLine(except.Message)
   End Try
End Sub

Private Sub DisconnectCompleted(result As IAsyncResult)
    Dim client As AsyncFtpClient = result.AsyncState
    
    Try
        ' All Begin calls must be matched by their End call.
        client.EndDisconnect(result)
    Catch except As Exception
        Console.WriteLine(except.Message)
    End Try
End Sub

	
AsyncFtpClient client = new AsyncFtpClient();

// When using the AsyncFtpClient class, you can instruct
// the library to automatically redirect events on the main UI thread
// by setting the SynchronizingObject property.
client.SynchronizingObject = m_resultForm;

try
{
    // Start connecting asynchronously.
    IAsyncResult result = client.BeginConnect( hostname, new AsyncCallback( ConnectCompleted ), client );
}
catch( Exception except )
{
    console.WriteLine( except.Message );
}

private void ConnectCompleted( IAsyncResult result )
{
    AsyncFtpClient client = result.AsyncState as AsyncFtpClient;
    
    try
    {
        // All Begin calls must be matched by their End call.
        client.EndConnect( result );
         
        // Now that we are connected, let's login anonymously, but again using the async method call.
        client.BeginLogin( new AsyncCallback( LoginCompleted ), client );
    }
    catch( Exception except )
    {
        Console.WriteLine( except.Message );
    }
}

private void LoginCompleted( IAsyncResult result )
{
    AsyncFtpClient client = result.AsyncState as AsyncFtpClient;
    
    try
    {
        // All Begin calls must be matched by their End call.
        client.EndLogin( result );
        
        // Finally, we disconnect asynchronously. You can see that if you
        // wish to perform more operations, a cascading series of async
        // calls can get out of hand.
        client.BeginDisconnect( new AsyncCallback( DisconnectCompleted ), client );
        
     }
     catch( Exception except )
     {
         Console.WriteLine( except.Message );
     }
}

private void DisconnectCompleted( IAsyncResult result )
{
   AsyncFtpClient client = result.AsyncState as AsyncFtpClient;
   
   try
   {
       // All Begin calls must be matched by their End call.
       client.EndDisconnect( result );
   }
   catch( Exception except )
   {
       Console.WriteLine( except.Message );
   }
}
' If you don't mind pumping messages explicitly, this alternative 
' use of AsyncFtpClient can prove very useful to get readable code.
Dim client As New AsyncFtpClient()

' You can instruct the library to automatically redirect events on the main UI thread
' by setting the SynchronizingObject property.
client.SynchronizingObject = m_resultForm

Try
    ' Start connecting asynchronously. No need for a callback!
    Dim result As IAsyncResult = client.BeginConnect(hostname, Nothing, Nothing)
    
    ' Wait for that async result to complete, making sure to pump messages.
    While Not result.IsCompleted
        Application.DoEvents()
    End While
      
    ' Complete the connection. All "Begin" calls must be matched
    ' by their matching "End" method call.
    client.EndConnect(result)
    
    ' Now that we are connected, we can login.
    result = client.BeginLogin(Nothing, Nothing)
    
    While Not result.IsCompleted
        Application.DoEvents()
    End While 
    
    client.EndLogin(result)
    
    ' And finally, we disconnect.
    result = client.BeginDisconnect(Nothing, Nothing)
    
    While Not result.IsCompleted
        Application.DoEvents()
    End While 
    
    client.EndDisconnect(result)
Catch except As Exception
    Console.WriteLine(except.Message)
End Try
AsyncFtpClient client = new AsyncFtpClient();

// When using the AsyncFtpClient class, you can instruct
// the library to automatically redirect events on the main UI thread
// by setting the SynchronizingObject property.
client.SynchronizingObject = m_resultsForm;

try
{
    // Start connecting asynchronously. No need for a callback!
    IAsyncResult result = client.BeginConnect( hostname, null, null );
    
    // Wait for that async result to complete, making sure to pump messages.
    while( !result.IsCompleted )
        Application.DoEvents();
        
    // Complete the connection. All "Begin" calls must be matched by their matching "End" method call.
    client.EndConnect( result );
    
    // Now that we are connected, we can login.
    result = client.BeginLogin( null, null );
    
    while( !result.IsCompleted )
        Application.DoEvents();
        
    client.EndLogin( result );
    
    // And finally, we disconnect.
    result = client.BeginDisconnect( null, null );
    
    while( !result.IsCompleted )
        Application.DoEvents();
        
    client.EndDisconnect( result );
}    
catch( Exception except )
{
    Console.WriteLine( except.Message );
}
Inheritance Hierarchy

System.Object
   Xceed.Ftp.FtpClient
      Xceed.Ftp.AsyncFtpClient

Requirements

Target Platforms: Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

See Also

Reference

AsyncFtpClient Members
Xceed.Ftp Namespace