ComponentPro UltimateSftp

      Use asynchronous methods

      Language Filter: AllSend comments on this topic to ComponentPro

      Scp 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 SCP server and download a file asynchronously:

      Task-based approach

      using System;
      using ComponentPro.Net;
      
      ...
      
      // Create a new class instance.
      Scp client = new Scp();
      
      // Connect to the SCP server.
      client.Connect("localhost");
      
      // Authenticate.
      client.Authenticate("test", "test");
      
      // ... 
       
      // Download remote file '/test.dat' to 'c:\test.dat' 
      await client.DownloadFileAsync("/test.dat", "c:\\test.dat");
      
      // ... 
       
      // Disconnect.
      client.Disconnect();
      

      Event-based approach

      using System;
      using System.ComponentModel;
      using ComponentPro;
      using ComponentPro.Net;
      
      ...
      
      [STAThread]
      static void Main()
      {
          // Create a new class instance. 
          Scp client = new Scp();
      
          client.DownloadFileCompleted += client_DownloadFileCompleted;
      
          // Connect to the SCP server. 
          client.Connect("localhost");
      
          // Authenticate. 
          client.Authenticate("test", "test");
      
          // ... 
       
          // 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)
      {
          // Scp client = (Scp)sender; 
          if (e.Error != null)
              Console.WriteLine("Error: " + e.Error.ToString());
      }