Sample code for 30+ languages & platforms
Pascal (Lazarus/Delphi)

Wait for Async Method to Complete

See more Async Examples

Demonstrates using the Wait method to wait for an asynchronous method to complete. This example will do an SFTP upload (over SSH) and will use the Async version of each method. Obviously, waiting for the async method to complete is the same as making a synchronous call, but an application wouldn't typically do this. An application might, for example, do other things as a background task is running, and then later wait for the task to complete.

Chilkat Pascal (Lazarus/Delphi) Downloads

Pascal (Lazarus/Delphi)
program ChilkatDemo;

// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.

{$IFDEF FPC}
  {$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}

uses
  {$IFDEF UNIX}
  cthreads,
  {$ENDIF}
  SysUtils,
  CkDllLoader,
  Chilkat.SFtp,
  Chilkat.Task;

// ---------------------------------------------------------------------------

procedure RunDemo;
var
  success: Boolean;
  sftp: TSFtp;
  waitMaxMs: Integer;
  port: Integer;
  domain: string;
  task: TTask;
  remoteFilePath: string;
  localFilePath: string;

begin
  success := False;

  //  This example assumes the Chilkat API to have been previously unlocked.
  //  See Global Unlock Sample for sample code.

  sftp := TSFtp.Create;

  //  Set some timeouts, in milliseconds:
  sftp.ConnectTimeoutMs := 15000;
  sftp.IdleTimeoutMs := 15000;
  waitMaxMs := 30000;

  //  Connect to the SSH server.  
  //  The standard SSH port = 22
  //  The hostname may be a hostname or IP address.
  port := 22;
  domain := 'sftp.example.com';

  task := sftp.ConnectAsync(domain,port);
  if (sftp.LastMethodSuccess = False) then
    begin
      WriteLn(sftp.LastErrorText);
      Exit;
    end;
  //  Start the background task.
  success := task.Run();
  if (not success) then
    begin
      WriteLn(task.LastErrorText);
    end;

  //  Wait for the connect task to finish.
  //  The True/False returned by Wait applies to the Wait method call, not the task.
  success := task.Wait(waitMaxMs);
  if (not success or (task.StatusInt <> 7) or (task.TaskSuccess <> True)) then
    begin
      if (not success) then
        begin
          //  The task.LastErrorText applies to the Wait method call.
          WriteLn(task.LastErrorText);
        end
      else
        begin
          //  The ResultErrorText applies to the underlying task method call (i.e. the Connect)
          WriteLn(task.Status);
          WriteLn(task.ResultErrorText);
        end;
      task.Free;
      Exit;
    end;

  task.Free;

  //  Authenticate with the SSH server.  Chilkat SFTP supports
  //  both password-based authenication as well as public-key
  //  authentication.  This example uses password authenication.
  task := sftp.AuthenticatePwAsync('myLogin','myPassword');
  //  To keep the example short, we'll skip handling failures.
  //  The failures would be handled in the same way as shown above.

  success := task.Run();
  success := task.Wait(waitMaxMs);
  task.Free;

  //  After authenticating, the SFTP subsystem must be initialized:
  task := sftp.InitializeSftpAsync();
  success := task.Run();
  success := task.Wait(waitMaxMs);
  task.Free;

  //  Upload from the local file to the SSH server.
  //  Important -- the remote filepath is the 1st argument,
  //  the local filepath is the 2nd argument;
  remoteFilePath := 'hamlet.xml';
  localFilePath := 'c:/temp/hamlet.xml';

  task := sftp.UploadFileByNameAsync(remoteFilePath,localFilePath);
  if (sftp.LastMethodSuccess = False) then
    begin
      WriteLn(sftp.LastErrorText);
      Exit;
    end;

  success := task.Run();
  success := task.Wait(waitMaxMs);
  if (not success or (task.StatusInt <> 7) or (task.TaskSuccess <> True)) then
    begin
      if (not success) then
        begin
          //  The task.LastErrorText applies to the Wait method call.
          WriteLn(task.LastErrorText);
        end
      else
        begin
          //  The ResultErrorText applies to the underlying task method call (i.e. the Connect)
          WriteLn(task.Status);
          WriteLn(task.ResultErrorText);
        end;
      task.Free;
      Exit;
    end;

  task.Free;
  WriteLn('File uploaded.');


  sftp.Free;

end;

// ---------------------------------------------------------------------------

begin

  try
    RunDemo;
  except
    on E: Exception do
      WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
  end;

  WriteLn;
  {$IFDEF MSWINDOWS}
  WriteLn('Press Enter to exit...');
  ReadLn;
  {$ENDIF}
end.