Sample code for 30+ languages & platforms
AutoIt

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

AutoIt
Local $bSuccess = False

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

$oSftp = ObjCreate("Chilkat.SFtp")

; Set some timeouts, in milliseconds:
$oSftp.ConnectTimeoutMs = 15000
$oSftp.IdleTimeoutMs = 15000
Local $iWaitMaxMs = 30000

; Connect to the SSH server.  
; The standard SSH port = 22
; The hostname may be a hostname or IP address.
Local $iPort = 22
Local $sDomain = "sftp.example.com"

Local $oTask = $oSftp.ConnectAsync($sDomain,$iPort)
If ($oSftp.LastMethodSuccess = False) Then
    ConsoleWrite($oSftp.LastErrorText & @CRLF)
    Exit
EndIf

; Start the background task.
$bSuccess = $oTask.Run()
If (Not $bSuccess) Then
    ConsoleWrite($oTask.LastErrorText & @CRLF)
EndIf

; Wait for the connect task to finish.
; The True/False returned by Wait applies to the Wait method call, not the task.
$bSuccess = $oTask.Wait($iWaitMaxMs)
If (Not $bSuccess Or ($oTask.StatusInt <> 7) Or ($oTask.TaskSuccess <> True)) Then
    If (Not $bSuccess) Then
        ; The task.LastErrorText applies to the Wait method call.
        ConsoleWrite($oTask.LastErrorText & @CRLF)
    Else
        ; The ResultErrorText applies to the underlying task method call (i.e. the Connect)
        ConsoleWrite($oTask.Status & @CRLF)
        ConsoleWrite($oTask.ResultErrorText & @CRLF)
    EndIf

    Exit
EndIf

; Authenticate with the SSH server.  Chilkat SFTP supports
; both password-based authenication as well as public-key
; authentication.  This example uses password authenication.
$oTask = $oSftp.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.

$bSuccess = $oTask.Run()
$bSuccess = $oTask.Wait($iWaitMaxMs)

; After authenticating, the SFTP subsystem must be initialized:
$oTask = $oSftp.InitializeSftpAsync()
$bSuccess = $oTask.Run()
$bSuccess = $oTask.Wait($iWaitMaxMs)

; Upload from the local file to the SSH server.
; Important -- the remote filepath is the 1st argument,
; the local filepath is the 2nd argument;
Local $sRemoteFilePath = "hamlet.xml"
Local $sLocalFilePath = "c:/temp/hamlet.xml"

$oTask = $oSftp.UploadFileByNameAsync($sRemoteFilePath,$sLocalFilePath)
If ($oSftp.LastMethodSuccess = False) Then
    ConsoleWrite($oSftp.LastErrorText & @CRLF)
    Exit
EndIf

$bSuccess = $oTask.Run()
$bSuccess = $oTask.Wait($iWaitMaxMs)
If (Not $bSuccess Or ($oTask.StatusInt <> 7) Or ($oTask.TaskSuccess <> True)) Then
    If (Not $bSuccess) Then
        ; The task.LastErrorText applies to the Wait method call.
        ConsoleWrite($oTask.LastErrorText & @CRLF)
    Else
        ; The ResultErrorText applies to the underlying task method call (i.e. the Connect)
        ConsoleWrite($oTask.Status & @CRLF)
        ConsoleWrite($oTask.ResultErrorText & @CRLF)
    EndIf

    Exit
EndIf

ConsoleWrite("File uploaded." & @CRLF)