Xceed .NET Libraries Documentation
WaitingForAsyncOperation event (FTP-only)

The WaitingForAsyncOperation event is obsolete. The SynchronizingObject property should be used instead.

The WaitingForAsyncOperation event is raised when an FTP connection is taking place and the main thread is waiting for completion of an asynchroneous operation on another thread. Use the WaitingForAsyncOperation event to ensure that your application remains responsive (allowing Windows Forms events) while it waits for the completion of the operations associated with making a secure FTP connection.

Basic steps

To subscribe to the WaitingForAsyncOperation event, the following steps must be performed:

  1. Create an instance of the FtpConnection class. 

  2. Subscribe to the WaitingForAsyncOperation event.

The following example demonstrates how use the WaitingForAsyncOperation event in conjunction with the CertificateReceived event. For a description of how certificates and SSL connections work, see Secure FTP (SSL/TLS).

VB.NET Copy Code

Imports Xceed.FileSystem
Imports Xceed.Ftp

Dim connection As New FtpConnection(ftp.myplace.com", 990, "ftp_user_name", "ftp_user_name's remote password", AuthenticationMethod.Tls, VerificationFlags.None, Nothing, DataChannelProtection.Private, True)
Try
' In order to consult the server certificate, we must advise
' for the CertificateReceived event.
  AddHandler connection.CertificateReceived, AddressOf OnCertificateReceived
' And to allow WinForms actions within the above event, we'll need
' to redirect the call to the main thread. To achieve this, we must
' make sure the application keeps pumping messages.
  AddHandler connection.WaitingForAsyncOperation, AddressOf OnWaitingForAsyncOperation
Dim folder As New FtpFolder(connection)

  For Each item As FileSystemItem In folder.GetItems(True)
    Console.WriteLine(item.FullName)
  Next item
Finally
  CType(connection, IDisposable).Dispose()
End Try

Private Sub DisplayServerCertificate(ByVal sender As Object, ByVal e As CertificateReceivedEventArgs)
  ' The Status argument property tells you if the server certificate was accepted
  ' based on the VerificationFlags you provided.
  If e.Status <> VerificationStatus.ValidCertificate Then
    Console.WriteLine("The server certificate is invalid: {0}", e.Status.ToString())
    Console.WriteLine(e.ServerCertificate.ToString())
    ' You have three choices here:
    '
    '   1) Refuse the certificate by setting e.Action to VerificationAction.Reject,
    '      thus making the authentication fail. This is e.Action's default value
    '      when the server certificate isn't valid.
    '
    '   2) Set e.Flags to less restrictive criterion and ask the library to
    '      validate the certificate again by setting e.Action to
    '      VerificationAction.VerifyAgain.
    '
    '   3) Force the library to accept this certificate by setting e.Action to
    '      VerificationAction.Accept.
    '
    ' We'll do #1 or #3, depending on the user's answer.
    Console.WriteLine("Do you want to accept this certificate anyway? [Y/N]")
    Dim answer As Integer = Console.Read()
    If Microsoft.VisualBasic.ChrW(answer) = "y"c _
    Or Microsoft.VisualBasic.ChrW(answer) = "Y"c Then
      e.Action = VerificationAction.Accept
    End If
  Else
    ' e.Action's default value is VerificationAction.Accept
    Console.WriteLine("Valid certificate received from server.")
  End If
End Sub

Private Sub OnCertificateReceived(ByVal sender As Object, ByVal e As CertificateReceivedEventArgs)
  ' IMPORTANT: This event occurs from one of the I/O threads. This means you
  ' can't perform GUI operations from within this event handler. To overcome
  ' this, you can handle the WaitingForAsyncOperation event (see below) to make
  ' sure the main thread is pumping messages, and redirect this event to the main
  ' thread via ISynchronizeInvoke.Invoke, implemented by your form.
  If Me.InvokeRequired Then
    Me.Invoke(New CertificateReceivedEventHandler(AddressOf Me.DisplayServerCertificate), New Object() {sender, e})
  Else
    Me.DisplayServerCertificate(sender, e)
  End If
End Sub

Private Sub OnWaitingForAsyncOperation(ByVal sender As Object, ByVal e As EventArgs)
  ' We simply make sure any blocking operation keeps pumping messages.
  Application.DoEvents()
End Sub

C# Copy Code

using System;
using System.Windows.Forms;
using Xceed.Ftp;
 
using (FtpConnection connection = new FtpConnection( "ftp.myplace.com", 990,
                                                     "ftp_user_name",
                                                     "ftp_user_name's remote password",
                                                     AuthenticationMethod.Tls,
                                                     VerificationFlags.None, null,
                                                     DataChannelProtection.Private, true) )
{
  // In order to consult the server certificate, we must advise
  // for the CertificateReceived event.
  connection.CertificateReceived += new CertificateReceivedEventHandler(this.OnCertificateReceived);
       
  // And to allow WinForms actions within the above event, we'll need
  // to redirect the call to the main thread. To achieve this, we must
  // make sure the application keeps pumping messages.
  connection.WaitingForAsyncOperation += new EventHandler( this.OnWaitingForAsyncOperation );
 
  FtpFolder folder = new FtpFolder( connection );

  foreach( FileSystemItem item in folder.GetItems( true ) )
  {
    Console.WriteLine( item.FullName );
  }
}
 
private void DisplayServerCertificate( object sender, CertificateReceivedEventArgs e )
{
   // The Status argument property tells you if the server certificate was accepted
   // based on the VerificationFlags you provided.
   if(   e.Status != VerificationStatus.ValidCertificate )
   {
     Console.WriteLine( "The server certificate is invalid: {0}", e.Status.ToString());
     Console.WriteLine( e.ServerCertificate.ToString() );
     // You have three choices here:
     //
     //   1) Refuse the certificate by setting e.Action to VerificationAction.Reject,
     //      thus making the authentication fail. This is e.Action's default value
     //      when the server certificate isn't valid.
     //
     //   2) Set e.Flags to less restrictive criterion and ask the library to
     //      validate the certificate again by setting e.Action to
     //      VerificationAction.VerifyAgain.
     //
     //   3) Force the library to accept this certificate by setting e.Action to
     //      VerificationAction.Accept.
     //
     // We'll do #1 or #3, depending on the user's answer.
     Console.WriteLine( "Do you want to accept this certificate anyway? [Y/N]" );
     int answer = Console.Read();

     if( ( answer == 'y' ) || ( answer == 'Y' ) )
     {
       e.Action = VerificationAction.Accept;
     }
   }
   else
   {
     // e.Action's default value is VerificationAction.Accept
     Console.WriteLine("Valid certificate received from server.");
   }    
}
 
private void OnCertificateReceived( object sender, CertificateReceivedEventArgs e   )
{
  // IMPORTANT: This event occurs from one of the I/O threads. This means you
  // can't perform GUI operations from within this event handler. To overcome
  // this, you can handle the WaitingForAsyncOperation event (see below) to make
  // sure the main thread is pumping messages, and redirect this event to the main
  // thread via ISynchronizeInvoke.Invoke, implemented by your form.
  if( this.InvokeRequired )
  {
    this.Invoke( new CertificateReceivedEventHandler( this.DisplayServerCertificate ),
                 new object[] { sender, e } );
  }
  else
  {
    this.DisplayServerCertificate( sender, e );
  }
}
 
private void OnWaitingForAsyncOperation( object sender, EventArgs e )
{
  // We simply make sure any blocking operation keeps pumping messages.
  Application.DoEvents();
}