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

Abort a Running SSH Tunnel Operation

See more SSH Tunnel Examples

Demonstrates the AbortCurrent property, which requests that the currently running foreground operation be aborted. It is intended to be set to true from another thread — for example, to interrupt a synchronous Connect that is waiting for an unresponsive server.

Background: A synchronous call like Connect blocks the calling thread until it finishes or times out, which is a problem when a user clicks "Cancel" or the app needs to shut down. Setting AbortCurrent from a different thread breaks the blocked operation out early, returning a failure the caller can handle. Note its scope is deliberately limited to the current foreground operation: it does not stop the background listener or terminate established tunnel clients — use StopAccepting or CloseTunnel for those.

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.SshTunnel;

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

procedure RunDemo;
var
  success: Boolean;
  tunnel: TSshTunnel;
  sshPort: Integer;
  waitForThreadExit: Boolean;

begin
  success := False;

  //  Demonstrates the SshTunnel.AbortCurrent property, which requests that the currently running
  //  foreground operation be aborted.

  tunnel := TSshTunnel.Create;

  //  AbortCurrent is intended to be set to True from ANOTHER thread to interrupt a synchronous
  //  operation in progress -- for example, a Connect that is waiting for an unresponsive server.
  //  It does not stop the background listener or terminate established background tunnel clients.
  //  
  //  The assignment below illustrates the property.  In a real application it would be set on a
  //  separate thread while Connect blocks; if it becomes True, the blocked Connect returns with a
  //  failure.
  tunnel.AbortCurrent := False;

  tunnel.DestHostname := 'db.internal.example.com';
  tunnel.DestPort := 5432;

  sshPort := 22;
  success := tunnel.Connect('ssh.example.com',sshPort);
  if (success = False) then
    begin
      WriteLn(tunnel.LastErrorText);
      Exit;
    end;
  WriteLn('Connected.');

  waitForThreadExit := True;
  success := tunnel.CloseTunnel(waitForThreadExit);
  if (success = False) then
    begin
      WriteLn(tunnel.LastErrorText);
      Exit;
    end;


  tunnel.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.