Sample code for 30+ languages & platforms
AutoIt

Async Methods Returning an Object

See more Async Examples

Demonstrates how to call an asynchronous method that returns an object. This example reads email from a POP3 server using the Async versions of the Chilkat methods.

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.

$bSuccess = False

$oMailman = ObjCreate("Chilkat.MailMan")

; Set the POP3 server's hostname
$oMailman.MailHost = "pop.example.com"

; Set the POP3 login/password and any other requirements..
$oMailman.PopUsername = "myLogin"
$oMailman.PopPassword = "myPassword"
$oMailman.PopSsl = True
$oMailman.MailPort = 995

; Connect to the POP3 server:
Local $oTask = $oMailman.Pop3BeginSessionAsync()
If ($oMailman.LastMethodSuccess = False) Then
    ConsoleWrite($oMailman.LastErrorText & @CRLF)
    Exit
EndIf

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

    Exit
EndIf

; Wait for the POP3 connect task to finish.
; The True/False returned by Wait applies to the Wait method call, not the task.
Local $iMaxWaitMs = 30000
$bSuccess = $oTask.Wait($iMaxWaitMs)
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 Pop3BeginSession)
        ConsoleWrite($oTask.Status & @CRLF)
        ConsoleWrite($oTask.ResultErrorText & @CRLF)
    EndIf

    Exit
EndIf

; Get the number of messages in the mailbox.
$oTask = $oMailman.GetMailboxCountAsync()

; 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($iMaxWaitMs)
Local $iNumMessages = $oTask.GetResultInt()

If ($iNumMessages = 0) Then
    Exit
EndIf

$oEmail = ObjCreate("Chilkat.Email")

Local $i
For $i = 1 To $iNumMessages

    $oTask = $oMailman.FetchByMsgnumAsync($i)
    If ($oMailman.LastMethodSuccess = False) Then
        ConsoleWrite($oMailman.LastErrorText & @CRLF)
        Exit
    EndIf

    $bSuccess = $oTask.Run()
    $bSuccess = $oTask.Wait($iMaxWaitMs)
    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 FetchByMsgnum)
            ConsoleWrite($oTask.Status & @CRLF)
            ConsoleWrite($oTask.ResultErrorText & @CRLF)
        EndIf

        Exit
    EndIf

    ; Each Chilkat object that can be a return value of an asynchronous task will
    ; have a method named LoadTaskResult.  The object returned in the underlying
    ; asynchronous method call is retrieved by calling LoadTaskResult. 

    ; To say it another way: The application will provide a pre-existing object of
    ; the desired return type (in this case it is an email object).  This object is
    ; loaded by calling LoadTaskResult.  

    $bSuccess = $oEmail.LoadTaskResult($oTask)

    If (Not $bSuccess) Then
        ConsoleWrite($oEmail.LastErrorText & @CRLF)
        Exit
    Else
        ConsoleWrite($oEmail.From & ": " & $oEmail.Subject & @LF & @CRLF)
    EndIf

Next