Sample code for 30+ languages & platforms
AutoIt

Async Task Chain

See more Async Examples

Demonstrates how to combine a sequence of asynchronous tasks into a single task chain to be run in a background thread.

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:
$bSuccess = $oMailman.Pop3BeginSession()
If (Not $bSuccess) Then
    ConsoleWrite($oMailman.LastErrorText & @CRLF)
    Exit
EndIf

; Get the number of messages in the mailbox.
Local $iNumMessages = $oMailman.GetMailboxCount()
If ($iNumMessages = 0) Then
    ConsoleWrite("No email messages in the POP3 mailbox." & @CRLF)
    Exit
EndIf

$oTaskChain = ObjCreate("Chilkat.TaskChain")

; Create async task objects to fetch each email message
; by its sequence number
Local $i
Local $oTask

For $i = 1 To $iNumMessages

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

    ; Append each task to the task chain.
    $bSuccess = $oTaskChain.Append($oTask)
    If (Not $bSuccess) Then
        ConsoleWrite($oTaskChain.LastErrorText & @CRLF)
        Exit
    EndIf

Next

; At this point, no tasks have actually started running.
; All we've done so far is to create tasks for the work that will be done
; when the task chain is run.

; Start the task chain running in a background thread.
; Each task is run one after the other (on the same background thread) until all tasks have completed.
; The task chain will stop at the first task that fails.
$oTaskChain.StopOnFailedTask = True
$bSuccess = $oTaskChain.Run()
If (Not $bSuccess) Then
    ConsoleWrite($oTaskChain.LastErrorText & @CRLF)
    Exit
EndIf

; The application is now free to do anything else
; while the emails are being downloaded

; For this example, we'll simply sleep and periodically
; check to see if the taskchain if finished. 
While $oTaskChain.Finished <> True

    ; Sleep 100 ms.
    $oTaskChain.SleepMs 100

Wend

; A finished task chain could be one that was canceled, aborted, or truly finished.  

; If the task chain "completed", then it ran to completion.  A "completed" task will
; have a StatusInt equal to 7.   If the task finished, but was not completed, then it must've
; been aborted or canceled:
If ($oTaskChain.StatusInt <> 7) Then
    ConsoleWrite("Task did not complete." & @CRLF)
    ConsoleWrite("task chain status: " & $oTaskChain.Status & @CRLF)
    Exit
EndIf

$oEmail = ObjCreate("Chilkat.Email")

Local $iNumTasks = $oTaskChain.NumTasks
Local $iTaskIdx = 0

While ($iTaskIdx < $iNumTasks)

    $oTask = $oTaskChain.GetTask($iTaskIdx)

    ; Load the email object with email downloaded by this task.
    $bSuccess = $oEmail.LoadTaskResult($oTask)
    If (Not $bSuccess) Then
        ConsoleWrite("Failed to load email object for task." & @CRLF)
    Else
        ConsoleWrite($oEmail.From & "; " & $oEmail.Subject & @CRLF)
    EndIf

    $iTaskIdx = $iTaskIdx + 1
Wend