Sample code for 30+ languages & platforms
Delphi DLL

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 Delphi DLL Downloads

Delphi DLL
uses
    Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
    Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, SFtp, Task;

...

procedure TForm1.Button1Click(Sender: TObject);
var
success: Boolean;
sftp: HCkSFtp;
waitMaxMs: Integer;
port: Integer;
domain: PWideChar;
task: HCkTask;
remoteFilePath: PWideChar;
localFilePath: PWideChar;

begin
success := False;

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

sftp := CkSFtp_Create();

// Set some timeouts, in milliseconds:
CkSFtp_putConnectTimeoutMs(sftp,15000);
CkSFtp_putIdleTimeoutMs(sftp,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 := CkSFtp_ConnectAsync(sftp,domain,port);
if (CkSFtp_getLastMethodSuccess(sftp) = False) then
  begin
    Memo1.Lines.Add(CkSFtp__lastErrorText(sftp));
    Exit;
  end;
// Start the background task.
success := CkTask_Run(task);
if (not success) then
  begin
    Memo1.Lines.Add(CkTask__lastErrorText(task));
  end;

// Wait for the connect task to finish.
// The True/False returned by Wait applies to the Wait method call, not the task.
success := CkTask_Wait(task,waitMaxMs);
if (not success or (CkTask_getStatusInt(task) <> 7) or (CkTask_getTaskSuccess(task) <> True)) then
  begin
    if (not success) then
      begin
        // The task.LastErrorText applies to the Wait method call.
        Memo1.Lines.Add(CkTask__lastErrorText(task));
      end
    else
      begin
        // The ResultErrorText applies to the underlying task method call (i.e. the Connect)
        Memo1.Lines.Add(CkTask__status(task));
        Memo1.Lines.Add(CkTask__resultErrorText(task));
      end;
    CkTask_Dispose(task);
    Exit;
  end;

CkTask_Dispose(task);

// Authenticate with the SSH server.  Chilkat SFTP supports
// both password-based authenication as well as public-key
// authentication.  This example uses password authenication.
task := CkSFtp_AuthenticatePwAsync(sftp,'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 := CkTask_Run(task);
success := CkTask_Wait(task,waitMaxMs);
CkTask_Dispose(task);

// After authenticating, the SFTP subsystem must be initialized:
task := CkSFtp_InitializeSftpAsync(sftp);
success := CkTask_Run(task);
success := CkTask_Wait(task,waitMaxMs);
CkTask_Dispose(task);

// 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 := CkSFtp_UploadFileByNameAsync(sftp,remoteFilePath,localFilePath);
if (CkSFtp_getLastMethodSuccess(sftp) = False) then
  begin
    Memo1.Lines.Add(CkSFtp__lastErrorText(sftp));
    Exit;
  end;

success := CkTask_Run(task);
success := CkTask_Wait(task,waitMaxMs);
if (not success or (CkTask_getStatusInt(task) <> 7) or (CkTask_getTaskSuccess(task) <> True)) then
  begin
    if (not success) then
      begin
        // The task.LastErrorText applies to the Wait method call.
        Memo1.Lines.Add(CkTask__lastErrorText(task));
      end
    else
      begin
        // The ResultErrorText applies to the underlying task method call (i.e. the Connect)
        Memo1.Lines.Add(CkTask__status(task));
        Memo1.Lines.Add(CkTask__resultErrorText(task));
      end;
    CkTask_Dispose(task);
    Exit;
  end;

CkTask_Dispose(task);
Memo1.Lines.Add('File uploaded.');

CkSFtp_Dispose(sftp);

end;