Sample code for 30+ languages & platforms
PowerBuilder

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 PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Tunnel
integer li_SshPort
integer li_WaitForThreadExit

li_Success = 0

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

loo_Tunnel = create oleobject
li_rc = loo_Tunnel.ConnectToNewObject("Chilkat.SshTunnel")
if li_rc < 0 then
    destroy loo_Tunnel
    MessageBox("Error","Connecting to COM object failed")
    return
end if

//  AbortCurrent is intended to be set to 1 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 1, the blocked Connect returns with a
//  failure.
loo_Tunnel.AbortCurrent = 0

loo_Tunnel.DestHostname = "db.internal.example.com"
loo_Tunnel.DestPort = 5432

li_SshPort = 22
li_Success = loo_Tunnel.Connect("ssh.example.com",li_SshPort)
if li_Success = 0 then
    Write-Debug loo_Tunnel.LastErrorText
    destroy loo_Tunnel
    return
end if

Write-Debug "Connected."

li_WaitForThreadExit = 1
li_Success = loo_Tunnel.CloseTunnel(li_WaitForThreadExit)
if li_Success = 0 then
    Write-Debug loo_Tunnel.LastErrorText
    destroy loo_Tunnel
    return
end if



destroy loo_Tunnel